Categories
Python Answers

How to reverse a list in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *