8000 [3.11] GH-98363: Update batched() recipe in docs to match 3.12 (#100323) · python/cpython@0918667 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0918667

Browse files
authored
[3.11] GH-98363: Update batched() recipe in docs to match 3.12 (#100323)
1 parent bf0a334 commit 0918667

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Doc/library/itertools.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -881,12 +881,12 @@ which incur interpreter overhead.
881881
raise ValueError('Expected fill, strict, or ignore')
882882
883883
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."
885885
# batched('ABCDEFG', 3) --> ABC DEF G
886886
if n < 1:
887887
raise ValueError('n must be at least one')
888888
it = iter(iterable)
889-
while (batch := list(islice(it, n))):
889+
while (batch := tuple(islice(it, n))):
890890
yield batch
891891

892892
def triplewise(iterable):
@@ -1248,25 +1248,25 @@ which incur interpreter overhead.
12481248
[('a', 'b', 'c'), ('d', 'e', 'f')]
12491249

12501250
>>> list(batched('ABCDEFG', 3))
1251-
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
1251+
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]
12521252
>>> list(batched('ABCDEF', 3))
1253-
[['A', 'B', 'C'], ['D', 'E', 'F']]
1253+
[('A', 'B', 'C'), ('D', 'E', 'F')]
12541254
>>> list(batched('ABCDE', 3))
1255-
[['A', 'B', 'C'], ['D', 'E']]
1255+
[('A', 'B', 'C'), ('D', 'E')]
12561256
>>> list(batched('ABCD', 3))
1257-
[['A', 'B', 'C'], ['D']]
1257+
[('A', 'B', 'C'), ('D',)]
12581258
>>> list(batched('ABC', 3))
12 BF52 59-
[['A', 'B', 'C']]
1259+
[('A', 'B', 'C')]
12601260
>>> list(batched('AB', 3))
1261-
[['A', 'B']]
1261+
[('A', 'B')]
12621262
>>> list(batched('A', 3))
1263-
[['A']]
1263+
[('A',)]
12641264
>>> list(batched('', 3))
12651265
[]
12661266
>>> list(batched('ABCDEFG', 2))
1267-
[['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
1267+
[('A', 'B'), ('C', 'D'), ('E', 'F'), ('G',)]
12681268
>>> list(batched('ABCDEFG', 1))
1269-
[['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
1269+
[('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]
12701270
>>> s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
12711271
>>> all(list(flatten(batched(s[:n], 5))) == list(s[:n]) for n in range(len(s)))
12721272
True

0 commit comments

Comments
 (0)
0