[Python-checkins] [3.11] GH-98363: Update batched() recipe in docs to match 3.12 (#100323)

rhettinger webhook-mailer at python.org
Sat Dec 17 20:10:37 EST 2022


https://github.com/python/cpython/commit/09186676cf3d483d19d64f5f1ed23b5a7e6edbbb
commit: 09186676cf3d483d19d64f5f1ed23b5a7e6edbbb
branch: 3.11
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2022-12-17T19:10:03-06:00
summary:

[3.11] GH-98363: Update batched() recipe in docs to match 3.12 (#100323)

files:
M Doc/library/itertools.rst

diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index ee70b16f42fb..8eb843ab0a67 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -881,12 +881,12 @@ which incur interpreter overhead.
            raise ValueError('Expected fill, strict, or ignore')
 
    def batched(iterable, n):
-       "Batch data into lists of length n. The last batch may be shorter."
+       "Batch data into tuples of length n. The last batch may be shorter."
        # batched('ABCDEFG', 3) --> ABC DEF G
        if n < 1:
            raise ValueError('n must be at least one')
        it = iter(iterable)
-       while (batch := list(islice(it, n))):
+       while (batch := tuple(islice(it, n))):
            yield batch
 
    def triplewise(iterable):
@@ -1248,25 +1248,25 @@ which incur interpreter overhead.
     [('a', 'b', 'c'), ('d', 'e', 'f')]
 
     >>> list(batched('ABCDEFG', 3))
-    [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
+    [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]
     >>> list(batched('ABCDEF', 3))
-    [['A', 'B', 'C'], ['D', 'E', 'F']]
+    [('A', 'B', 'C'), ('D', 'E', 'F')]
     >>> list(batched('ABCDE', 3))
-    [['A', 'B', 'C'], ['D', 'E']]
+    [('A', 'B', 'C'), ('D', 'E')]
     >>> list(batched('ABCD', 3))
-    [['A', 'B', 'C'], ['D']]
+    [('A', 'B', 'C'), ('D',)]
     >>> list(batched('ABC', 3))
-    [['A', 'B', 'C']]
+    [('A', 'B', 'C')]
     >>> list(batched('AB', 3))
-    [['A', 'B']]
+    [('A', 'B')]
     >>> list(batched('A', 3))
-    [['A']]
+    [('A',)]
     >>> list(batched('', 3))
     []
     >>> list(batched('ABCDEFG', 2))
-    [['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
+    [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G',)]
     >>> list(batched('ABCDEFG', 1))
-    [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
+    [('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]
     >>> s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     >>> all(list(flatten(batched(s[:n], 5))) == list(s[:n]) for n in range(len(s)))
     True



More information about the Python-checkins mailing list