Skip to content

Instantly share code, notes, and snippets.

@lihuanshuai
Last active September 28, 2017 07: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 lihuanshuai/b6b47000f25bac7bfcc4e3ef4a1f97f6 to your computer and use it in GitHub Desktop.
Save lihuanshuai/b6b47000f25bac7bfcc4e3ef4a1f97f6 to your computer and use it in GitHub Desktop.
from itertools import tee, islice as slice, chain, izip as zip
def previous_and_next(iterable, fill_value=None):
prevs, items, nexts = tee(iterable, 3)
prevs = chain([fill_value], prevs)
nexts = chain(slice(nexts, 1, None), [fill_value])
return zip(prevs, items, nexts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment