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
itertools.grouper #50271
Comments
An itertool to Group-by-n >>> lst = range(15)
>>> itertools.grouper(lst, 5)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]] This function is often asked in several c.l.py discussions, such as these: There are several issues. What should be done if the number of items in
or should there be separate functions for each of them? What about infinite list? Most recipes for the function uses zip which |
This has been rejected before.
|
All implementations relying on zip or zip_longest breaks with infinite And it is not impossible to define a clean, flexible, and familiar API
It should also be weighed that a lot of people are expecting for this |
How is it broken? >>> def grouper(n, iterable, fillvalue=None):
... args = [iter(iterable)] * n
... return zip_longest(*args, fillvalue=fillvalue)
>>> g = grouper(3, count())
>>> next(g)
(0, 1, 2)
>>> next(g)
(3, 4, 5)
>>> next(g)
(6, 7, 8)
>>> next(g)
Of course, it's possible. I find that to be bad design. Generally, we |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: