Use the Rolling or sliding window iterator in Python solution:
>>> 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
...
>>> path = window({1, 2, 3, 4})
>>> for step in gen:
... print path
(1, 2)
(2, 3)
(3, 4)
This happens to follow sorted order, because for python integers hash(x) == x
and thus the sequence of 1, 2, 3, 4 is inserted in that order into the set.