Skip to content

Commit

Permalink
Use match/case in grouper() recipe (gh-113059)
Browse files Browse the repository at this point in the history
Use match/case in grouper() reciper
  • Loading branch information
rhettinger authored Dec 13, 2023
1 parent 480b4b3 commit 2111795
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -914,14 +914,15 @@ which incur interpreter overhead.
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
args = [iter(iterable)] * n
if incomplete == 'fill':
return zip_longest(*args, fillvalue=fillvalue)
elif incomplete == 'strict':
return zip(*args, strict=True)
elif incomplete == 'ignore':
return zip(*args)
else:
raise ValueError('Expected fill, strict, or ignore')
match incomplete:
case 'fill':
return zip_longest(*args, fillvalue=fillvalue)
case 'strict':
return zip(*args, strict=True)
case 'ignore':
return zip(*args)
case _:
raise ValueError('Expected fill, strict, or ignore')
def sliding_window(iterable, n):
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
Expand Down

0 comments on commit 2111795

Please sign in to comment.