Skip to content

Instantly share code, notes, and snippets.

@vegarsti
Last active February 27, 2018 10:46
Show Gist options
  • Save vegarsti/908132d8e93f6bfb54e4624da217f4ce to your computer and use it in GitHub Desktop.
Save vegarsti/908132d8e93f6bfb54e4624da217f4ce to your computer and use it in GitHub Desktop.
def pairwise(it):
iterator = iter(it)
try:
i = next(iterator)
except StopIteration:
return
yield
try:
j = next(iterator)
except StopIteration:
return
yield
yield(i, j)
for element in iterator:
i, j = j, element
yield(i, j)
i = pairwise([0])
for z in i:
print(z)
print()
"""
"""
i = pairwise([0, 1])
for z in i:
print(z)
print()
"""
(0, 1)
"""
i = pairwise([0, 1, 2])
for z in i:
print(z)
"""
(0, 1)
(1, 2)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment