Add consume() recipe to itertools docs.

This commit is contained in:
Raymond Hettinger 2009-03-09 11:58:33 +00:00
parent d31d294e89
commit fb30cca591
1 changed files with 4 additions and 0 deletions

View File

@ -635,6 +635,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)