Skip to content

Instantly share code, notes, and snippets.

@AndyHoang
Created June 2, 2017 02:40
Show Gist options
  • Save AndyHoang/1047c1af651adee050133821c9ca6975 to your computer and use it in GitHub Desktop.
Save AndyHoang/1047c1af651adee050133821c9ca6975 to your computer and use it in GitHub Desktop.
pre next from generator
#https://stackoverflow.com/questions/1011938/python-previous-and-next-values-inside-a-loop
from itertools import tee, islice, chain, izip
def previous_and_next(some_iterable):
prevs, items, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return izip(prevs, items, nexts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment