Add itertools.batched() #98363
Copy link
Copy link
Closed
Labels
3.12only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement
Description
This was requested on python-ideas:
def batched(iterable, n):
"Batch data into lists of length n. The last batch may be shorter."
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError('n must be >= 1')
it = iter(iterable)
while (batch := list(islice(it, n))):
yield batch
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement
Activity
GH-98363: Add itertools.batched() (GH-98364)
GH-98363: Presize the list for batched() (GH-98419)
Merge branch 'main' into typewatch
GH-98363: Fix exception handling in batched() (GH-98523)
GH-98363: Use better accessor macro (#98535)
pythonGH-98363: Slicing isn't necessary. A resize will suffice.
GH-98363: Slicing isn't necessary. A size reduction will suffice. (GH…
GH-98363: Have batched() return tuples (GH-100118)
pythonGH-98363: Shrink the physical size as well as the logical size
GH-98363: Shrink the physical size as well as the logical size (GH-10…
[3.11] GH-98363: Update batched() recipe in docs to match 3.12 (#100323)