From 3f186337078517d724bf754b16ac683a3cb984e6 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 13 Dec 2023 12:11:52 -0600 Subject: [PATCH] Use match/case in grouper() recipe (gh-113059) Use match/case in grouper() reciper (cherry picked from commit 2111795d0c2dea0ade67d5d76f839102d68caa23) Co-authored-by: Raymond Hettinger --- Doc/library/itertools.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index fc25e51678ac69..03127afe1b4460 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -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