Python-Ref > Data structures > List > Reversing order of items
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Reversing order of items

The task at hand is easily accomplished using the lists method reverse.
Expand/Shrink
Zdroj: (list8-1.py)
  1   list1 = range( 20)
  2   list2 = ["one", "two", "three", "four"]
  3   
  4   print list1
  5   list1.reverse()
  6   print list1
  7   print
  8   
  9   
 10   print list2
 11   list2.reverse()
 12   print list2
 13   list2.reverse()
 14   print list2
stdout:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

['one', 'two', 'three', 'four']
['four', 'three', 'two', 'one']
['one', 'two', 'three', 'four']
Doba běhu: 21.6 ms
Please note that the reversal is done in place replacing the old content of the list.