Examples of how to reverse a list in python:
Table of contents
Inverse a list with reverse()
To modify a list by reversing the order of the elements, a solution is to use the function reverse(), example
>>> l = ['a','b','c','d','e','f']
>>> l.reverse()
>>> l
['f', 'e', 'd', 'c', 'b', 'a']
Inverse a list with reversed()
To iterate through a list in the reversed order, a solution is to use the function reversed(), example:
>>> l = ['a','b','c','d','e','f']
>>> for i in reversed(l):
... print i
...
f
e
d
c
b
a
>>> l
['a', 'b', 'c', 'd', 'e', 'f']
Note that the function reversed() to note modify the list.
References
Links | Site |
---|---|
Data Structures | python doc |
6 PEP 322: Reverse Iteration | python doc |
Traverse a list in reverse order in Python | stackoverflow |
How can I reverse a list in python? | stackoverflow |
Reverse a string in Python | stackoverflow |