Reversing a List in Python

To reverse a list in Python, you can use the built-in methods list.reverse() and list.reversed(). The list.reverse() method reverses the elements of the original list (in-place) rather than creating a new one. The list.reversed() method returns a reverse iterator of the specified sequence without changing the original list. If you only want to iterate over the list elements in reverse order, it is preferable to use the list.reversed() function, as it is faster than rearranging the elements in place. You can also use slicing to flip and create a new list without changing the original. In this Python example, we use the list.reverse() method to iterate over the list elements in reverse order. Click Execute to run Python Reverse List Example online and see the result.
Reversing a List in Python Execute
my_list = ['Python', 'PHP', 'Java', 'JavaScript']

my_list.reverse()

print(my_list)
Updated: Viewed: 2234 times

How to reverse a list using the reverse() method in Python?

You can reverse the list using the list.reverse() method. The method takes no arguments and does not return any value. It updates the existing list.

Python List reverse() Example
my_list = ['Python', 'PHP', 'Java', 'JavaScript']

my_list.reverse()

print(my_list)

# ['JavaScript', 'Java', 'PHP', 'Python']

How to use reversed() method in Python?

You can use the reversed() method to create a reverse iterator and then use it with the list() function to create a new list with items in reverse order.

Python List reversed() Example
my_list = [1, 2, 4, 3, 5]

print(list(reversed(my_list)))

# [5, 3, 4, 2, 1]

How to reverse a list using slicing in Python?

You can reverse a list by slicing in Python using the [::-1] notation. Slicing the list results in a new list containing the retrieved items, and the original list remains unchanged.

Python Reverse List with Slicing Example
my_list = [1, 2, 3, 4]

print(my_list[::-1])

# [4, 3, 2, 1]

See also