What is the most efficient way to rotate a list in python? Right now I have something like this:
>>> def rotate(l, n): ... return l[n:] + l[:n]
If you just want to iterate over these sets of elements rather than construct a separate data structure, consider using iterators to construct a generator expression:
def shift(l,n): return itertools.islice(itertools.cycle(l),n,n+len(l)) >>> list(shift([1,2,3],1)) [2, 3, 1]