User Tools

Site Tools


niftypythoncommands

Getting the contents of a list in reverse order.

Say you have a list like this

areversedlist=“AF,AE,AD,AC,AB,AA,Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A”

And you'd actually like to get the contents from the end of the list this is how you could do it:

areversedlist.split(',')[::-1]

The command above would give you the contents of the list only from back to front

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF']

This could come in quite handy if you have long lists and you don't want to manually split them up in order to reverse them.

niftypythoncommands.txt · Last modified: 2012/03/09 10:43 (external edit)