List - rotate

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jan 23 12:13:59 CET 2002


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> wrote in 
news:nf0t4ugt1qio7r34eg392st617h6qk9nc1 at 4ax.com:

>>For a string I do the following to Rotate Right:
>>
>>s = "abc"
>>s[1:]+s[0]
>>>> s = "bca"
>>
>>which works correctly.
>>
>>Tried the following for list, does not seem to work.
>>a = [1,2,3]
>>a[1:].append(a[0])
>>
> 
To mutate a list you should use pop and append.
    	a.append(a.pop(0))
To create a new, rotated list you can use similar code to your string. The 
following should work for any sequence type including list, tuple and 
string:
    	s = s[1:]+s[:1]

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list