Skip to content

Instantly share code, notes, and snippets.

@egnha
Last active December 13, 2019 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egnha/fe5e2cff838b2e8f7f46a6d07584eb87 to your computer and use it in GitHub Desktop.
Save egnha/fe5e2cff838b2e8f7f46a6d07584eb87 to your computer and use it in GitHub Desktop.
Lazy consecutive pairs
from itertools import islice, tee
def pairs(xs):
"""a, b, c, d, ... -> (a, b), (c, d), ..."""
fst, snd = (islice(_xs, start, None, 2) for start, _xs in enumerate(tee(xs)))
return zip(fst, snd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment