How to Reverse Python List?

Python – Reverse List – reverse()

To reverse the order of items in a list, or simply said reverse a list, use reverse() method of List Class.

In this tutorial, we shall first discuss about the trivial reverse() function to reverse a Python List. Then we shall go through some of the unconventional approaches to reverse a list, for widening our Python knowledge.

Syntax – reverse()

The syntax of reverse() method is given below.

mylist.reverse()

Another method of reversing a list is using slicing. Following statement reverses and returns the resulting list.

reversed_list = mylist[::-1]

Example 1: Reverse List using reverse()

In the following example, we have a List of numbers. We will reverse the list using reverse() method.

Python Program

#list of numbers
mylist = [21, 5, 8, 52, 21, 87, 52]
#reverse list
mylist.reverse()
#print the list
print(mylist)
Run

Output

[52, 87, 21, 52, 8, 5, 21]

reverse() method directly updates the original list.

Example 2: Reverse List using Slicing

In this example, we will use slicing technique and revere the given list.

Python Program

#list of numbers
mylist = [21, 5, 8, 52, 21, 87, 52]
#reverse list using slicing
mylist = mylist[::-1]
#print the list
print(mylist)
Run

Output

[52, 87, 21, 52, 8, 5, 21]

Slicing does not modify the original list, but returns a reversed list.

Example 3: Reverse List of Strings

In this example, we reverse a list of strings.

Python Program

#list of strings
mylist = ['list', 'dict', 'set']
#reverse list
mylist.reverse()
#print the list
print(mylist)
Run

Output

['set', 'dict', 'list']

Summary

In this tutorial of Python Examples, we learned how to reverse a Python List, with the help of well detailed example programs.