Skip to content

Commit 270a091

Browse files
authored
Improve grouper() recipe to demonstrate all forms of zip() (GH-30837)
1 parent f7955a8 commit 270a091

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

Doc/library/itertools.rst

+12-3
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,20 @@ which incur interpreter overhead.
813813
return starmap(func, repeat(args))
814814
return starmap(func, repeat(args, times))
815815

816-
def grouper(iterable, n, fillvalue=None):
816+
def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
817817
"Collect data into non-overlapping fixed-length chunks or blocks"
818-
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
818+
# grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
819+
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
820+
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
819821
args = [iter(iterable)] * n
820-
return zip_longest(*args, fillvalue=fillvalue)
822+
if incomplete == 'fill':
823+
return zip_longest(*args, fillvalue=fillvalue)
824+
if incomplete == 'strict':
825+
return zip(*args, strict=True)
826+
if incomplete == 'ignore':
827+
return zip(*args)
828+
else:
829+
raise ValueError('Expected fill, strict, or ignore')
821830
822831
def triplewise(iterable):
823832
"Return overlapping triplets from an iterable"

Lib/test/test_itertools.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -2436,6 +2436,21 @@ def test_permutations_sizeof(self):
24362436
... else:
24372437
... return starmap(func, repeat(args, times))
24382438
2439+
>>> def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
2440+
... "Collect data into non-overlapping fixed-length chunks or blocks"
2441+
... # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
2442+
... # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
2443+
... # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
2444+
... args = [iter(iterable)] * n
2445+
... if incomplete == 'fill':
2446+
... return zip_longest(*args, fillvalue=fillvalue)
2447+
... if incomplete == 'strict':
2448+
... return zip(*args, strict=True)
2449+
... if incomplete == 'ignore':
2450+
... return zip(*args)
2451+
... else:
2452+
... raise ValueError('Expected fill, strict, or ignore')
2453+
24392454
>>> def triplewise(iterable):
24402455
... "Return overlapping triplets from an iterable"
24412456
... # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
@@ -2453,11 +2468,6 @@ def test_permutations_sizeof(self):
24532468
... window.append(x)
24542469
... yield tuple(window)
24552470
2456-
>>> def grouper(n, iterable, fillvalue=None):
2457-
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
2458-
... args = [iter(iterable)] * n
2459-
... return zip_longest(*args, fillvalue=fillvalue)
2460-
24612471
>>> def roundrobin(*iterables):
24622472
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
24632473
... # Recipe credited to George Sakkis
@@ -2626,9 +2636,22 @@ def test_permutations_sizeof(self):
26262636
>>> dotproduct([1,2,3], [4,5,6])
26272637
32
26282638
2629-
>>> list(grouper(3, 'abcdefg', 'x'))
2639+
>>> list(grouper('abcdefg', 3, fillvalue='x'))
26302640
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
26312641
2642+
>>> it = grouper('abcdefg', 3, incomplete='strict')
2643+
>>> next(it)
2644+
('a', 'b', 'c')
2645+
>>> next(it)
2646+
('d', 'e', 'f')
2647+
>>> next(it)
2648+
Traceback (most recent call last):
2649+
...
2650+
ValueError: zip() argument 2 is shorter than argument 1
2651+
2652+
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
2653+
[('a', 'b', 'c'), ('d', 'e', 'f')]
2654+
26322655
>>> list(triplewise('ABCDEFG'))
26332656
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
26342657

0 commit comments

Comments
 (0)