Skip to content

Commit 07cc997

Browse files
authored
[3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446)
1 parent 30b9c4d commit 07cc997

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

Doc/library/itertools.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,8 @@ which incur interpreter overhead.
887887
def batched(iterable, n):
888888
"Batch data into lists of length n. The last batch may be shorter."
889889
# batched('ABCDEFG', 3) --> ABC DEF G
890+
if n < 1:
891+
raise ValueError('n must be at least one')
890892
it = iter(iterable)
891893
while (batch := list(islice(it, n))):
892894
yield batch
@@ -1272,12 +1274,6 @@ which incur interpreter overhead.
12721274
[['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
12731275
>>> list(batched('ABCDEFG', 1))
12741276
[['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
1275-
>>> list(batched('ABCDEFG', 0))
1276-
[]
1277-
>>> list(batched('ABCDEFG', -1))
1278-
Traceback (most recent call last):
1279-
...
1280-
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
12811277
>>> s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
12821278
>>> all(list(flatten(batched(s[:n], 5))) == list(s[:n]) for n in range(len(s)))
12831279
True

0 commit comments

Comments
 (0)