Q:

Wrapping around a python list as a slice operation

Consider the following simple python code
>>> L = range(3)
>>> L
[0, 1, 2]

We can take slices of this array as follows:
>>> L[1:3]
[1, 2]

Is there any way to wrap around the above array by shifting to the left
[1, 2, 0]

by simply using slice operations?

A:

Rotate left n elements (or right for negative n):
L = L[n:] + L[:n]

Note that collections.deque has support for rotations. It might be better to use that instead of lists.

A:

Left:
L[:1], L[1:] = L[-1:], L[:-1]

Right:
L[-1:], L[:-1] = L[:1], L[1:]

