User Tools

Site Tools


niftypythoncommands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

niftypythoncommands [2012/03/09 10:43]
niftypythoncommands [2012/03/09 10:43] (current)
Line 1: Line 1:
  
 +
 +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)