def pairwise(lst): """ yield item i and item i+1 in lst. e.g. (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None) """ if not lst: return #yield None, lst[0] for i in range(len(lst)-1): yield lst[i], lst[i+1] yield lst[-1], None