Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use match/case in grouper() recipe #113059

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading