Python looping: idiomatically comparing successive items in a list
Question: I need to loop over a list of objects, comparing them like this: 0 vs. 1, 1
vs. 2, 2 vs. 3, etc. (I'm using pysvn to extract a list of diffs.) I wound up
just looping over an index, but I keep wondering if there's some way to do it
which is more closely idiomatic. It's Python; shouldn't I be using iterators
in some clever way? Simply looping over the index seems pretty clear, but I
wonder if there's a more expressive or concise way to do it.

    
    
    for revindex in xrange(len(dm_revisions) - 1):
        summary = \
            svn.diff_summarize(svn_path,
                              revision1=dm_revisions[revindex],
                              revision2 = dm_revisions[revindex+1])
    


Answer: This is called a sliding window. There's an [example in the `itertools`
documentation](http://www.python.org/doc/2.3.5/lib/itertools-example.html)
that does it. Here's the code:

    
    
    from itertools import islice
    
    def window(seq, n=2):
        "Returns a sliding window (of width n) over data from the iterable"
        "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
        it = iter(seq)
        result = tuple(islice(it, n))
        if len(result) == n:
            yield result    
        for elem in it:
            result = result[1:] + (elem,)
            yield result
    

What that, you can say this:

    
    
    for r1, r2 in window(dm_revisions):
        summary = svn.diff_summarize(svn_path, revision1=r1, revision2=r2)
    

Of course you only care about the case where n=2, so you can get away with
something much simpler:

    
    
    def adjacent_pairs(seq):
        it = iter(seq)
        a = it.next()
        for b in it:
            yield a, b
            a = b
    
    for r1, r2 in adjacent_pairs(dm_revisions):
        summary = svn.diff_summarize(svn_path, revision1=r1, revision2=r2)
    

