Rotating lists?

Jeffrey Froman jeffrey at fro.man
Wed Sep 15 18:45:23 EDT 2004


Ivan Voras wrote:
> I need to transform this:
> 
> [1,2,3]
> 
> into this:
> 
> [2,3,1]

If in-place transformation is OK, you could use:

L = [1,2,3]
L.append(L.pop(0))   # left rotation
L.insert(0, L.pop()) # right rotation



Jeffrey



More information about the Python-list mailing list