Question: 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]
... 
>>> l = [1,2,3,4]
>>> rotate(l,1)
[2, 3, 4, 1]
>>> rotate(l,2)
[3, 4, 1, 2]
>>> rotate(l,0)
[1, 2, 3, 4]
>>> rotate(l,-1)
[4, 1, 2, 3]

```

Is there a better way?

Answer: 
I also got interested in this and compared some of the suggested solutions with [perfplot](https://github.com/nschloe/perfplot) (a small project of mine).

It turns out that Kelly Bundy's suggestion

```py
tmp = data[shift:]
tmp += data[:shift]

```

performs very well for all shifts.

Essentially, perfplot performs the shift for increasing large arrays and measures the time. Here are the results:

`shift = 1`:

[![enter image description here](https://i.stack.imgur.com/ES8ia.png)](https://i.stack.imgur.com/ES8ia.png)

`shift = 100`:

[![enter image description here](https://i.stack.imgur.com/cBifV.png)](https://i.stack.imgur.com/cBifV.png)

---

Code to reproduce the plot:

```py
import numpy
import perfplot
import collections

shift = 100

def list_append(data):
    return data[shift:] + data[:shift]

def list_append2(data):
    tmp = data[shift:]
    tmp += data[:shift]
    return tmp

def shift_concatenate(data):
    return numpy.concatenate([data[shift:], data[:shift]])

def roll(data):
    return numpy.roll(data, -shift)

def collections_deque(data):
    items = collections.deque(data)
    items.rotate(-shift)
    return items

def pop_append(data):
    data = data.copy()
    for _ in range(shift):
        data.append(data.pop(0))
    return data

b = perfplot.bench(
    setup=lambda n: numpy.random.rand(n).tolist(),
    kernels=[
        list_append,
        list_append2,
        roll,
        shift_concatenate,
        collections_deque,
        pop_append,
    ],
    n_range=[2 ** k for k in range(7, 20)],
    xlabel="len(data)",
)
b.show()
b.save("shift100.png")

```
If efficiency is your goal, (cycles? memory?) you may be better off looking at the array module: <http://docs.python.org/library/array.html>

Arrays do not have the overhead of lists. 

As far as pure lists go though, what you have is about as good as you can hope to do.