Iterate through pairs of items in a Python list
From https://stackoverflow.com/questions/5764782/iterate-through-pairs-of-items-in-a-python-list
from itertools import tee, izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
for v, w in pairwise(a):
...