Skip to content

Instantly share code, notes, and snippets.

@addam
Created Sep 22, 2016
Embed
What would you like to do?
iterate over consecutive pairs
def pairs(seq, cyclic=True):
it = iter(seq)
first = prev = next(it)
for here in it:
yield prev, here
prev = here
if cyclic:
yield here, first
# I use this snippet in almost every script
# It is really simple but still I would prefer to import it from itertools if it was there...
# If you know of a more elegant way to do this, please give me a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment