]> granicus.if.org Git - python/commitdiff
Add consume() recipe to itertools docs.
authorRaymond Hettinger <python@rcn.com>
Mon, 9 Mar 2009 11:57:29 +0000 (11:57 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 9 Mar 2009 11:57:29 +0000 (11:57 +0000)
Doc/library/itertools.rst

index f68465a4a88610001980eccf4444fb6c57590a51..1e946b11b0fdd7912c8c0e9c3330d17e7df0c31a 100644 (file)
@@ -701,6 +701,10 @@ which incur interpreter overhead.
        "Return function(0), function(1), ..."
        return imap(function, count(start))
 
+   def consume(iterator, n):
+       "Advance the iterator n-steps ahead. If n is none, consume entirely."
+       collections.deque(islice(iterator, n), maxlen=0)
+
    def nth(iterable, n, default=None):
        "Returns the nth item or a default value"
        return next(islice(iterable, n, None), default)