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 offset(iter, n=1, pad=None): i1, i2 = itertools.tee(iter) i1_padded = itertools.chain(itertools.repeat(pad, n), i1) return itertools.izip(i1_padded, i2)
@bpgergo + @user792036 = this. Best of two worlds :).