look ahead one item during iteration

Iteration is a fundamental Python idiom. It is simple and effective.

for n in iterable: # do something with n

But there are also cases when you might want to look ahead one item during iteration. For example, with a sorted list, one can eliminate duplicated items by dropping those equals to the next item. This generator based recipe that enumerate an item and its next in a list. For example,

>>> for i,j in pairwise([1,2,3]): print i,j
...
1 2
2 3
3 None

A use case of pairwise is to remove duplicated items in a sorted list. Below is a very compact example to do this using pairwise and list comprehension.

>>> [i for i,j in pairwise([1,1,1,2,3,3]) if j==None or i!=j]
[1, 2, 3]

Compare this with various examples in the 'Remove duplicates from a sequence' recipe. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560

Note that you can easily customize the initial and the final item iterated, that is whether you want (None, item0) as the initial item and (itemN, None) as the last item. I find it most useful to have only (itemN, None) included. In this case the number of steps iterated is the same as in ordinary list iteration.

Alternatively this could be expressed even more concisely using zip(lst, lst[1:]+[None])

Also the pairwise recipe in the itertools module (Python Library Reference 5.16.3) does something similar.

However I find the generator recipe both easier to understand as well as most straightforward to control the inital and final item iterated.

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

code.activestate.com ↗