Lists are ordered collection of objects in python. It may happen that you require to reverse the order of elements in a list. There are a number of ways of reversing a list in python. In this tutorial, we’ll cover how to reverse a list using different ways.
Before we proceed, here’s a quick refresher on python lists – Lists are used to store an ordered collection of items. These items can be any type of object from numbers to strings or even another list. This makes lists are one of the most versatile data structures in python to store a collection of objects. For more, check out our guide on lists and other data structures in python.
How to reverse a list in python?
There are multiples ways of reversing a list in python. You can use the list function reverse()
which reverses the list in-place, you can use the built-in python function reversed()
used to reverse any iterable, or you can use a neat slicing trick without invoking any functions.
The reverse() function
reverse()
is a list function in python used to reverse the contents of a list in-place. That is, it modifies the original list. The following is the syntax:
sample_list.reverse()
Here, sample_list is the list to be reversed. reverse()
does not take any arguments. It modifies the list in-place and does not return any value (It returns None
)
Example: Reverse a list using the reverse() function
# reverse a list using reverse()
ls = ['India', 'USA', 'Japan', 'Australia']
print("Original list:", ls)
# reverse the list
ls.reverse()
print("After reversing:", ls)
Output:
Original list: ['India', 'USA', 'Japan', 'Australia']
After reversing: ['Australia', 'Japan', 'USA', 'India']
In the above example, the list ls
is reversed using the list function reverse()
. We can see that the function modifies the list in-place from the output.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
The reversed() function
reversed()
is a built-in python function that works not only on lists but on any iterable. It returns an iterator which allows us to access the elements of the iterable in a reversed order. The returned iterator can be converted to a list using the list()
function. The following is the syntax:
reversed(sequence)
Here, sequence is the iterable (example – list, tuple, string, etc) to be reversed.
Example: Reverse a list using the reversed() function
# reverse a list using reversed()
ls = ['India', 'USA', 'Japan', 'Australia']
# reverse the list
ls_reversed = list(reversed(ls))
print("Original list:", ls)
print("After reversing:", ls_reversed)
Output:
Original list: ['India', 'USA', 'Japan', 'Australia']
After reversing: ['Australia', 'Japan', 'USA', 'India']
In the above example, the elements of the list ls
are reversed using the reversed()
function and the reversed list is saved as ls_reversed
. We see that the reversed()
function does not modify the list in-place, rather it returns an iterator of reversed items which is then converted to a list using the list()
function.
The slicing trick
You can also reverse a list using the slicing operator. The following is the syntax:
sample_list[::-1]
Here, sample_list is the list to be reversed. It utilizes the ability to slice a list using a step (example, list[start:stop:step]
. Using the slicing operator with a -1
step gives the reversed list.
The above syntax returns a new list with the items reversed and does not modify the original list.
# reverse a list using slicing
ls = ['India', 'USA', 'Japan', 'Australia']
# reverse the list
ls_reversed = ls[::-1]
print("Original list:", ls)
print("After reversing:", ls_reversed)
Output:
Original list: ['India', 'USA', 'Japan', 'Australia']
After reversing: ['Australia', 'Japan', 'USA', 'India']
In the above example, the list ls
is reversed using the slicing operator with a step of -1
. The slice returns a new list with the items reversed. The original list remains unchanged.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.