summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-01-23 22:02:31 (GMT)
committerGitHub <noreply@github.com>2022-01-23 22:02:31 (GMT)
commitb2c7fe1f61c8ec3742635428570bc61d820c7a68 (patch)
treec68903363ec582d0707b61678d24728e0b02b332 /Doc/library
parente3ade66ec575e0cb4882cfdff155ef962e67c837 (diff)
downloadcpython-b2c7fe1f61c8ec3742635428570bc61d820c7a68.zip
cpython-b2c7fe1f61c8ec3742635428570bc61d820c7a68.tar.gz
cpython-b2c7fe1f61c8ec3742635428570bc61d820c7a68.tar.bz2
Improve grouper() recipe to demonstrate all forms of zip() (GH-30837) (GH-30840)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/itertools.rst15
1 files changed, 12 insertions, 3 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 61d8b86..3466756 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -813,11 +813,20 @@ which incur interpreter overhead.
return starmap(func, repeat(args))
return starmap(func, repeat(args, times))
- def grouper(iterable, n, fillvalue=None):
+ def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
"Collect data into non-overlapping fixed-length chunks or blocks"
- # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
+ # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
+ # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
+ # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
args = [iter(iterable)] * n
- return zip_longest(*args, fillvalue=fillvalue)
+ if incomplete == 'fill':
+ return zip_longest(*args, fillvalue=fillvalue)
+ if incomplete == 'strict':
+ return zip(*args, strict=True)
+ if incomplete == 'ignore':
+ return zip(*args)
+ else:
+ raise ValueError('Expected fill, strict, or ignore')
def triplewise(iterable):
"Return overlapping triplets from an iterable"