@@ -881,12 +881,12 @@ which incur interpreter overhead.
881
881
raise ValueError('Expected fill, strict, or ignore')
882
882
883
883
def batched(iterable, n):
884
- "Batch data into lists of length n. The last batch may be shorter."
884
+ "Batch data into tuples of length n. The last batch may be shorter."
885
885
# batched('ABCDEFG', 3) --> ABC DEF G
886
886
if n < 1:
887
887
raise ValueError('n must be at least one')
888
888
it = iter(iterable)
889
- while (batch := list (islice(it, n))):
889
+ while (batch := tuple (islice(it, n))):
890
890
yield batch
891
891
892
892
def triplewise(iterable):
@@ -1248,25 +1248,25 @@ which incur interpreter overhead.
1248
1248
[('a', 'b', 'c'), ('d', 'e', 'f')]
1249
1249
1250
1250
>>> list (batched(' ABCDEFG' , 3 ))
1251
- [[ 'A', 'B', 'C'], [ 'D', 'E', 'F'], [ 'G'] ]
1251
+ [( 'A', 'B', 'C'), ( 'D', 'E', 'F'), ( 'G',) ]
1252
1252
>>> list (batched(' ABCDEF' , 3 ))
1253
- [[ 'A', 'B', 'C'], [ 'D', 'E', 'F'] ]
1253
+ [( 'A', 'B', 'C'), ( 'D', 'E', 'F') ]
1254
1254
>>> list (batched(' ABCDE' , 3 ))
1255
- [[ 'A', 'B', 'C'], [ 'D', 'E'] ]
1255
+ [( 'A', 'B', 'C'), ( 'D', 'E') ]
1256
1256
>>> list (batched(' ABCD' , 3 ))
1257
- [[ 'A', 'B', 'C'], [ 'D'] ]
1257
+ [( 'A', 'B', 'C'), ( 'D',) ]
1258
1258
>>> list (batched(' ABC' , 3 ))
12
BF52
59
- [[ 'A', 'B', 'C'] ]
1259
+ [( 'A', 'B', 'C') ]
1260
1260
>>> list (batched(' AB' , 3 ))
1261
- [[ 'A', 'B'] ]
1261
+ [( 'A', 'B') ]
1262
1262
>>> list (batched(' A' , 3 ))
1263
- [[ 'A'] ]
1263
+ [( 'A',) ]
1264
1264
>>> list (batched(' ' , 3 ))
1265
1265
[]
1266
1266
>>> list (batched(' ABCDEFG' , 2 ))
1267
- [[ 'A', 'B'], [ 'C', 'D'], [ 'E', 'F'], [ 'G'] ]
1267
+ [( 'A', 'B'), ( 'C', 'D'), ( 'E', 'F'), ( 'G',) ]
1268
1268
>>> list (batched(' ABCDEFG' , 1 ))
1269
- [[ 'A'], [ 'B'], [ 'C'], [ 'D'], [ 'E'], [ 'F'], [ 'G'] ]
1269
+ [( 'A',), ( 'B',), ( 'C',), ( 'D',), ( 'E',), ( 'F',), ( 'G',) ]
1270
1270
>>> s = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1271
1271
>>> all (list (flatten(batched(s[:n], 5 ))) == list (s[:n]) for n in range (len (s)))
1272
1272
True
0 commit comments