I would like an enumerate-like functional on iterators which yields the pair (previous_element, current_element). That is, given that iter>
enumerate
(previous_element, current_element)
iter>
def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2, s3), ... see http://docs.python.org/library/itertools.html """ a, b = itertools.tee(iterable) b.next() return itertools.izip(a, b)
EDIT moved doc string into the function