summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-03-28 18:25:01 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-03-28 18:25:01 (GMT)
commitb8d688cd7c8b7d267cb84f2431457b71e52628fb (patch)
treeba868160a4976a56875dae729417659faba13f1c /Doc
parentd282b931a3d7b33fcddff15904b465ec8d54ba8f (diff)
downloadcpython-b8d688cd7c8b7d267cb84f2431457b71e52628fb.zip
cpython-b8d688cd7c8b7d267cb84f2431457b71e52628fb.tar.gz
cpython-b8d688cd7c8b7d267cb84f2431457b71e52628fb.tar.bz2
Update itertools recipe for consume().
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/itertools.rst8
1 files changed, 7 insertions, 1 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 4f82a27..f83e976 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"