Sometimes, we want to reverse a list in Python.
In this article, we’ll look at how to reverse a list in Python.
How to reverse a list in Python?
To reverse a list in Python, we can use the reversed
function.
For instance, we write:
array = [0, 10, 20, 40]
l = list(reversed(array))
print(l)
We call reversed
with array
to return an iterator with the entries in array
reversed.
Then we convert that back to a list with list
.
Therefore, l
is [40, 20, 10, 0]
.
Conclusion
To reverse a list in Python, we can use the reversed
function.