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: 
A [`collections.deque`](http://docs.python.org/library/collections.html#deque-objects) is optimized for pulling and pushing on both ends. They even have a dedicated `rotate()` method. 

```
from collections import deque
items = deque([1, 2])
items.append(3)        # deque == [1, 2, 3]
items.rotate(1)        # The deque is now: [3, 1, 2]
items.rotate(-1)       # Returns deque to original state: [1, 2, 3]
item = items.popleft() # deque == [2, 3]

```
I take this cost model as a reference:

<http://scripts.mit.edu/~6.006/fall07/wiki/index.php?title=Python_Cost_Model>

Your method of slicing the list and concatenating two sub-lists are linear-time operations. I would suggest using pop, which is a constant-time operation, e.g.:

```
def shift(list, n):
    for i in range(n)
        temp = list.pop()
        list.insert(0, temp)

```