From: Raymond Hettinger Date: Sun, 28 Mar 2010 18:25:01 +0000 (+0000) Subject: Update itertools recipe for consume(). X-Git-Tag: v2.7b1~189 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b8d688cd7c8b7d267cb84f2431457b71e52628fb;p=python Update itertools recipe for consume(). --- diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 4f82a27d11..f83e9762a2 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -672,7 +672,13 @@ which incur interpreter overhead. def consume(iterator, n): "Advance the iterator n-steps ahead. If n is none, consume entirely." - collections.deque(islice(iterator, n), maxlen=0) + # Use functions that consume iterators at C speed. + if n is None: + # feed the entire iterator into a zero-length deque + collections.deque(iterator, maxlen=0) + else: + # advance to the emtpy slice starting at position n + next(islice(iterator, n, n), None) def nth(iterable, n, default=None): "Returns the nth item or a default value"