]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] Docs: Improve example for ``itertools.batched()`` (GH-136775) (#136779)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 19 Jul 2025 09:35:48 +0000 (11:35 +0200)
committerGitHub <noreply@github.com>
Sat, 19 Jul 2025 09:35:48 +0000 (09:35 +0000)
Docs: Improve example for ``itertools.batched()`` (GH-136775)

The current example `batched('ABCDEFG', n=3) → ABC DEF G` can confuse readers because both, the size of the tuples and the number of tuples are 3.
By using a batch size of n=2, it is clearer that the `n` argument refers to the size of the resulting tuples.
I.e. the new example is: `batched('ABCDEFG', n=2) → AB CD EF G`
(cherry picked from commit 3eecc72ac70943f7e33297eea17803af15322c88)

Co-authored-by: RafaelWO <38643099+RafaelWO@users.noreply.github.com>
Doc/library/itertools.rst

index 51bbca2cf0f2d4ef011a9f3d602f2cf955624d4f..d07003ac2bb053bb443b4200e58861f0c4e96e3c 100644 (file)
@@ -47,7 +47,7 @@ Iterator            Arguments               Results
 Iterator                        Arguments                       Results                                             Example
 ============================    ============================    =================================================   =============================================================
 :func:`accumulate`              p [,func]                       p0, p0+p1, p0+p1+p2, ...                            ``accumulate([1,2,3,4,5]) → 1 3 6 10 15``
-:func:`batched`                 p, n                            (p0, p1, ..., p_n-1), ...                           ``batched('ABCDEFG', n=3) → ABC DEF G``
+:func:`batched`                 p, n                            (p0, p1, ..., p_n-1), ...                           ``batched('ABCDEFG', n=2) → AB CD EF G``
 :func:`chain`                   p, q, ...                       p0, p1, ... plast, q0, q1, ...                      ``chain('ABC', 'DEF') → A B C D E F``
 :func:`chain.from_iterable`     iterable                        p0, p1, ... plast, q0, q1, ...                      ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
 :func:`compress`                data, selectors                 (d[0] if s[0]), (d[1] if s[1]), ...                 ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
@@ -181,7 +181,7 @@ loops that truncate the stream.
    Roughly equivalent to::
 
       def batched(iterable, n, *, strict=False):
-          # batched('ABCDEFG', 3) → ABC DEF G
+          # batched('ABCDEFG', 2) → AB CD EF G
           if n < 1:
               raise ValueError('n must be at least one')
           iterator = iter(iterable)