@@ -775,10 +775,7 @@ which incur interpreter overhead.
775775 return sum(map(pred, iterable))
776776
777777 def pad_none(iterable):
778- """Returns the sequence elements and then returns None indefinitely.
779-
780- Useful for emulating the behavior of the built-in map() function.
781- """
778+ "Returns the sequence elements and then returns None indefinitely."
782779 return chain(iterable, repeat(None))
783780
784781 def ncycles(iterable, n):
@@ -850,6 +847,13 @@ which incur interpreter overhead.
850847 else:
851848 raise ValueError('Expected fill, strict, or ignore')
852849
850+ def batched(iterable, n):
851+ "Batch data into lists of length n. The last batch may be shorter."
852+ # batched('ABCDEFG', 3) --> ABC DEF G
853+ it = iter(iterable)
854+ while (batch := list(islice(it, n))):
855+ yield batch
856+
853857 def triplewise(iterable):
854858 "Return overlapping triplets from an iterable"
855859 # triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
@@ -1168,8 +1172,8 @@ which incur interpreter overhead.
11681172
11691173 >>> list (sieve(30 ))
11701174 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171- >>> small_primes = [2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 ]
1172- >>> all (list (sieve(n)) == [p for p in small_primes if p < n] for n in range (60 ))
1175+ >>> small_primes = [2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 ]
1176+ >>> all (list (sieve(n)) == [p for p in small_primes if p < n] for n in range (101 ))
11731177 True
11741178 >>> len (list (sieve(100 )))
11751179 25
@@ -1212,6 +1216,36 @@ which incur interpreter overhead.
12121216 >>> list (grouper(' abcdefg' , n = 3 , incomplete = ' ignore' ))
12131217 [('a', 'b', 'c'), ('d', 'e', 'f')]
12141218
1219+ >>> list (batched(' ABCDEFG' , 3 ))
1220+ [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
1221+ >>> list (batched(' ABCDEF' , 3 ))
1222+ [['A', 'B', 'C'], ['D', 'E', 'F']]
1223+ >>> list (batched(' ABCDE' , 3 ))
1224+ [['A', 'B', 'C'], ['D', 'E']]
1225+ >>> list (batched(' ABCD' , 3 ))
1226+ [['A', 'B', 'C'], ['D']]
1227+ >>> list (batched(' ABC' , 3 ))
1228+ [['A', 'B', 'C']]
1229+ >>> list (batched(' AB' , 3 ))
1230+ [['A', 'B']]
1231+ >>> list (batched(' A' , 3 ))
1232+ [['A']]
1233+ >>> list (batched(' ' , 3 ))
1234+ []
1235+ >>> list (batched(' ABCDEFG' , 2 ))
1236+ [['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
1237+ >>> list (batched(' ABCDEFG' , 1 ))
1238+ [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
1239+ >>> list (batched(' ABCDEFG' , 0 ))
1240+ []
1241+ >>> list (batched(' ABCDEFG' , - 1 ))
1242+ Traceback (most recent call last):
1243+ ...
1244+ ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
1245+ >>> s = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1246+ >>> all (list (flatten(batched(s[:n], 5 ))) == list (s[:n]) for n in range (len (s)))
1247+ True
1248+
12151249 >>> list (triplewise(' ABCDEFG' ))
12161250 [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
12171251
0 commit comments