How to reverse the order of list elements in python ?

Published: April 02, 2019

DMCA.com Protection Status

Examples of how to reverse a list in python:

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