[Python-ideas] Batching/grouping function for itertools
Steven D'Aprano
steve at pearwood.info
Sun Dec 8 17:38:55 CET 2013
On Sun, Dec 08, 2013 at 09:13:06AM -0600, Ron Adam wrote:
> Possibly function to take the next n items of an iterator without advancing
> it.
Fundamentally impossible. The best you can do it advance the iterator
but store the results for later use.
> Along with a function to advance an iterator n ahead without taking
> anything.
Too trivial to bother with. Just advance the iterator and throw the
result away.
def advance(it, n):
for _ in range(n):
next(it)
You can even do it as a one-linear, at the expense of readability:
{next(it) and None for _ in range(n)}.pop()
(The pop isn't really necessary, I just like the fact that it means the
expression evaluates as None.)
> These would be simpler and easier to maintain, and have a wider range of
> uses.
Maybe, maybe not, but they don't solve the problem that people keep
asking to be solved.
--
Steven
More information about the Python-ideas
mailing list