Rotating lists?

Jeffrey Froman jeffrey at fro.man
Thu Sep 16 00:45:23 CEST 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