Consume an iterable

Raymond Hettinger python at
Fri Jan 22 20:11:21 CET 2010

On Jan 22, 6:13 am, Muhammad Alkarouri <malkaro... at> wrote:
> In the python help for itertools, the following function is provided:
> def consume(iterator, n):
>     "Advance the iterator n-steps ahead. If n is none, consume
> entirely."
>     collections.deque(islice(iterator, n), maxlen=0)
> What is the advantage of using a collections.deque against, say, the
> following code?
> def consume(iterator, n):
>     for _ in islice(iterator, n): pass

The deque version is faster.


More information about the Python-list mailing list