Reverse a list in python banner

Python Reverse a List – With Examples

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.

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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.

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.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top