Skip to content

Instantly share code, notes, and snippets.

@fumieval
Created February 24, 2012 11:06
Show Gist options
  • Save fumieval/1900177 to your computer and use it in GitHub Desktop.
Save fumieval/1900177 to your computer and use it in GitHub Desktop.
def consume(iterator, n=None):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# 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 empty slice starting at position n
next(islice(iterator, n, n), None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment