Consume an iterable

Jan Kaliszewski zuo at chopin.edu.pl
Fri Jan 22 10:25:33 EST 2010


> 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

Probably the former is faster. But I haven't check it. If you are curious,
use timeit module...

Regards,
*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list