Skip to content

Commit b9c5ffe

Browse files
[3.12] Use match/case in grouper() recipe (gh-113059) (gh-113197)
1 parent 85ee49c commit b9c5ffe

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Doc/library/itertools.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -914,14 +914,15 @@ which incur interpreter overhead.
914914
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
915915
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
916916
args = [iter(iterable)] * n
917-
if incomplete == 'fill':
918-
return zip_longest(*args, fillvalue=fillvalue)
919-
elif incomplete == 'strict':
920-
return zip(*args, strict=True)
921-
elif incomplete == 'ignore':
922-
return zip(*args)
923-
else:
924-
raise ValueError('Expected fill, strict, or ignore')
917+
match incomplete:
918+
case 'fill':
919+
return zip_longest(*args, fillvalue=fillvalue)
920+
case 'strict':
921+
return zip(*args, strict=True)
922+
case 'ignore':
923+
return zip(*args)
924+
case _:
925+
raise ValueError('Expected fill, strict, or ignore')
925926
926927
def sliding_window(iterable, n):
927928
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG

0 commit comments

Comments
 (0)