or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-utilities.mdcombinatorics.mdcombining.mdcomparison.mdgrouping.mdindex.mdindexing.mditeration-utilities.mdlookahead.mdmathematical.mdrandom-operations.mdselecting.mdsequence-utilities.mdspecial-purpose.mdsummarizing.mduniqueness.mdutility-classes.mdwindowing.md

windowing.mddocs/

0

# Windowing Operations

1

2

Functions for creating sliding windows and overlapping views of data.

3

4

## Capabilities

5

6

### Basic Windowing

7

8

Create sliding windows over iterables.

9

10

```python { .api }

11

def windowed(iterable, n, fillvalue=None, step=1):

12

"""

13

Create sliding window of size n over iterable.

14

15

Args:

16

iterable: Input iterable to window

17

n: Window size

18

fillvalue: Value to use for padding incomplete windows

19

step: Step size between windows

20

21

Returns:

22

Iterator of n-tuples representing windows

23

"""

24

25

def windowed_complete(iterable, n):

26

"""

27

Create sliding windows, yielding only complete windows.

28

29

Args:

30

iterable: Input iterable to window

31

n: Window size

32

33

Returns:

34

Iterator of n-tuples (no incomplete windows)

35

"""

36

37

def sliding_window(iterable, n):

38

"""

39

Create sliding window of size n.

40

41

Args:

42

iterable: Input iterable to window

43

n: Window size

44

45

Returns:

46

Iterator of n-tuples representing sliding windows

47

"""

48

```

49

50

**Usage Examples:**

51

52

```python

53

from more_itertools import windowed, sliding_window

54

55

# Basic sliding window

56

data = [1, 2, 3, 4, 5]

57

windows = list(windowed(data, 3))

58

# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

59

60

# With fillvalue for incomplete windows

61

windows = list(windowed(data, 3, fillvalue='X'))

62

# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 'X'), (5, 'X', 'X')]

63

64

# Step size greater than 1

65

windows = list(windowed(data, 3, step=2))

66

# Result: [(1, 2, 3), (3, 4, 5)]

67

```

68

69

### Pairwise and N-wise Operations

70

71

Generate adjacent pairs, triples, and n-tuples.

72

73

```python { .api }

74

def pairwise(iterable):

75

"""

76

Return successive overlapping pairs from iterable.

77

78

Args:

79

iterable: Input iterable

80

81

Returns:

82

Iterator of 2-tuples (adjacent pairs)

83

"""

84

85

def triplewise(iterable):

86

"""

87

Return successive overlapping triples from iterable.

88

89

Args:

90

iterable: Input iterable

91

92

Returns:

93

Iterator of 3-tuples (adjacent triples)

94

"""

95

```

96

97

**Usage Examples:**

98

99

```python

100

from more_itertools import pairwise, triplewise

101

102

# Adjacent pairs

103

pairs = list(pairwise([1, 2, 3, 4, 5]))

104

# Result: [(1, 2), (2, 3), (3, 4), (4, 5)]

105

106

# Adjacent triples

107

triples = list(triplewise([1, 2, 3, 4, 5]))

108

# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

109

```

110

111

### Staggered Views

112

113

Create lagged or offset views of data.

114

115

```python { .api }

116

def stagger(iterable, offsets=(0, 1), longest=False, fillvalue=None):

117

"""

118

Create multiple offset iterators from single iterable.

119

120

Args:

121

iterable: Input iterable to stagger

122

offsets: Sequence of offset values

123

longest: If True, iterate until longest iterator exhausted

124

fillvalue: Value for padding when longest=True

125

126

Returns:

127

Iterator of tuples with staggered values

128

"""

129

```

130

131

**Usage Examples:**

132

133

```python

134

from more_itertools import stagger

135

136

# Basic staggering

137

data = [0, 1, 2, 3, 4, 5]

138

staggered = list(stagger(data, offsets=(0, 2)))

139

# Result: [(0, 2), (1, 3), (2, 4), (3, 5)]

140

141

# Multiple offsets

142

staggered = list(stagger(data, offsets=(0, 1, 2)))

143

# Result: [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

144

145

# With longest=True

146

staggered = list(stagger(data, offsets=(0, 2), longest=True, fillvalue=None))

147

# Result: [(0, 2), (1, 3), (2, 4), (3, 5), (4, None), (5, None)]

148

```

149

150

### Substring Operations

151

152

Generate substrings and substring-like operations.

153

154

```python { .api }

155

def substrings(iterable):

156

"""

157

Generate all non-empty contiguous subsequences.

158

159

Args:

160

iterable: Input iterable

161

162

Returns:

163

Iterator of all contiguous subsequences

164

"""

165

166

def substrings_indexes(seq, reverse=False):

167

"""

168

Generate all substrings with their start and end indexes.

169

170

Args:

171

seq: Input sequence

172

reverse: If True, generate in reverse order

173

174

Returns:

175

Iterator of (substring, start_index, end_index) tuples

176

"""

177

178

def subslices(iterable):

179

"""

180

Generate all contiguous subslices as lists.

181

182

Args:

183

iterable: Input iterable

184

185

Returns:

186

Iterator of lists (all contiguous subslices)

187

"""

188

```

189

190

**Usage Examples:**

191

192

```python

193

from more_itertools import substrings, substrings_indexes

194

195

# All substrings

196

subs = list(substrings("abc"))

197

# Result: [('a',), ('a', 'b'), ('a', 'b', 'c'), ('b',), ('b', 'c'), ('c',)]

198

199

# With indexes

200

subs_idx = list(substrings_indexes("abc"))

201

# Result: [('a', 0, 1), ('ab', 0, 2), ('abc', 0, 3), ('b', 1, 2), ('bc', 1, 3), ('c', 2, 3)]

202

```