def previous_current_next(items):
    extend = itertools.chain([None], items, [None])
    previous, current, next = itertools.tee(extend, 3)
    try:
        current.next()
        next.next()
        next.next()
    except StopIteration:
        pass
    return itertools.izip(previous, current, next)
From http://www.wordaligned.org/articles/zippy-triples-served-with-python
dataset/ETHPy150Open fabiocorneti/django-easytree/easytree/templatetags/easytree_tags.py/previous_current_next