How to Reverse a List in Python : Various Methods

In Python, a list is a data structure for storing multiple variables in a single variable. It is an arranged series of objects, with each item being of any various type, like numbers, characters, or other lists. Lists are modifiable, which implies that one can add, remove or alter elements in the list after the creation. Python has an inbuilt function that allows you to reverse the elements of a list. For this, you have to use the reverse() method. In this post, you will know how to reverse the list using various methods.

Methods to reverse a list in python

Let’s know all the methods you will use to reverse a list in python.

Method 1: Using the reverse() method

The reverse() method allows you to reverse the order of elements in a list. It is called on a list object and modifies the original list in place after that the elements are reversed.

my_list = [10, 20, 30, 40, 50]
my_list.reverse()
print(my_list)

Output

[50, 40, 30, 20, 10]

Method 2: Use slicing to create a new reversed list

Slicing is a technique for obtaining a subset of elements from a sequence object, e.g. a string, list, or tuple, using the “:” slice operator and start and end indices. This operator defines the range of indices that should be included in the slice, from the start index (the position of the first element to include) to the end index (the position of the first element not to include).

Run the below lines of code to reverse the list using slicing. Here you will use the ::-1 to reverse the list.

my_list = [10, 20, 30, 40, 50]
reversed_list = my_list[::-1]
print(reversed_list) 

Output

[50, 40, 30, 20, 10]

Method 3: Reverse a list in python using the reversed() function

The reversed() function in Python outputs a reverse iterator. After that, you can use it to traverse the elements of a sequence in a reverse sequence. This function accepts a sequence object like a list, tuple, or string as its argument. Lastly, it returns an iterator that gives the elements of the sequence in reverse order, one by one.

my_list = [10, 20, 30, 40, 50]
reversed_list = list(reversed(my_list))
print(reversed_list) 

Output

[50, 40, 30, 20, 10]

Conclusion

There are many built-in methods and functions provided by python for reversing a list or iterating over its elements in reverse order. The above are the methods to allow you to reverse the list. The reverse() method allows you to modify the original list in place by reversing its elements.
If you will use the reversed() function then it will return a reverse iterator object. After that, you can use this function to iterate all the elements in reverse order. In this tutorial, You also learn how to use slicing to create a new reversed list. You can choose any method depending on your use case on the above-described methods.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link