Lists are probably one of the most important and widely used data types in Python. A list in Python is a data-type that is used to store multiple values, elements, or other data types. Lists are changeable, can contain multiple duplicate values, and are ordered. It is also possible to also perform indexing and slicing with lists. The items in the lists have an order, so the elements remain in the same order in which they were inserted. Moreover, you can change the list, add, or remove elements from the list. 

Being such a popular data-type, you can perform multiple operations on lists and one of them is the reverse operation. Reversing a list means that it traverses the elements from the end to the beginning. While the reverse operation might not have direct use-cases, there might be several indirect applications of reversing a list in Python.

Python has an in-built function called reverse() which can be directly used to reverse a list. And this is probably the most efficient and direct method to do so. However, there are several other workarounds as well which this article will discuss. 

So with no further ado, let’s get started.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

1. Reversing a List using the Reverse() Method

The general syntax of the reverse() method is - 

list.reverse()

Please note that it invokes the reverse method on a list and does not take any input, arguments, or parameters. Also, it does not return any value but changes the original list. It simply inverts the order of elements in the list and is the most efficient method in terms of both performance and speed. Let’s see how to do so with an example.

def reverseList(inputList):

   inputList.reverse()

   return inputList

inputList = ["Joanna", "Chandler", "Monica", "Joey", "Jessica", "Sarah", "Phoebe", "Rose", "Rachel"]

print("The original list is - ", inputList)

print("The list after reversal is - ", reverseList(inputList)) 

Here, there is a defined function that takes as input a list of names. Then, it invoked the built-in reverse() method on this list which changes the list itself and reverses it. After that, it returned the same modified list. Let’s verify the output.

ReversingList_1

You can see that the list has been reversed successfully.

2. Reversing a List using Reversed() Method

There is also another method called reversed() which can be used to reverse any data-type such as a list, tuple, string, etc. The difference between the reverse() and reversed() method is that the reversed() method does not reverse the list in-place or modify it. Neither does it return another reversed copy of the original list. What it does is, it will return a reversed iterator that points to the last element to be used to iterate the list and get the elements in reversed order.

Let’s try to understand this with the help of an example.

def reverseList(inputList):

   return reversed(inputList)

inputList = ["Joanna", "Chandler", "Monica", "Joey", "Jessica", "Sarah", "Phoebe", "Rose", "Rachel"]

print("The original list is - ", inputList)

print("The list after reversal is - ", end="")

for name in reverseList(inputList):

   print(name, end=" ")

print("\n")

In the above example, the function reverseList returns the output of the reversed (inputList) which is an iterator object pointing to the end of the list. You saw the use of this iterator object to print the elements in the reversed order.

Let’s verify the output of the above program.

ReversingList_2

You can see that the program has printed all the elements of the list in reversed order and the original list is still preserved.

3. Reversing a List using Slicing

This method is a new trick to invert a list. It does not perform any sort of in-place modification. In fact, it creates a new copy of the list and is memory in-efficient. Let’s understand this with the help of an example.

def reverseList(inputList):

   reversedList = inputList[::-1]

   return reversedList

inputList = ["Joanna", "Chandler", "Monica", "Joey", "Jessica", "Sarah", "Phoebe", "Rose", "Rachel"]

print("The original list is - ", inputList)

print("The list after reversal is - ", reverseList(inputList))

In the above program, you saw the act of list slicing to invert the list. There is no defined start and stop values in the list slicing parameters. This means that it takes the default values which are the list start and end. For the step value, you saw the use of -1, which means that the traversal must start from the end to the beginning of the list. Let’s verify the output by executing the program.

ReversingList_3 

You can see that the list has been reversed using the list slicing technique. This is probably the fastest way to invert a list, however, it is memory inefficient as it copies the entire list.

4. Reversing a List using for loop

It is possible to use the simple for loop to reverse a list in Python. It is the easiest way to understand, however; it takes a lot more lines of code to perform the same operation. Let’s see how to reverse a list using the for loops.

def reverseList(inputList):

   reversedList = []

   for i in range(0, len(inputList)):

       reversedList.append(inputList[len(inputList)- 1 - i])

   return reversedList

inputList = ["Joanna", "Chandler", "Monica", "Joey", "Jessica", "Sarah", "Phoebe", "Rose", "Rachel"]

print("The original list is - ", inputList)

print("The list after reversal is - ", reverseList(inputList))

In the above program, you saw that it first created an empty list that will store all the values of the original list in reversed order. There is the use of a simple for loop to traverse all the elements of the original list one-by-one. In each iteration, you saw that it appended the element at ( len(inputList) - 1 - i ). Thus, for the first element at index 0, this value returns 9 - 1 - 0 = 8 which is the index of the last element and so on.

Let’s verify the output of this program.

ReversingList_4

Reversing a list in Python using for loop is a tedious task as compared to other methods and also consumes a lot of memory since a copy of the list has to be made and also, the length of the list needs to be calculated which increases the execution time.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

5. Reversing a List using List Comprehension

List comprehensions in Python is a helpful technique that helps you to create another list based on the values in the original list after the execution of certain operations on the original list. The general syntax of list comprehension in Python is - 

modifiedlist = [expression for item in iterable if condition == True]

It does not modify the original list but creates a new list.

You can use list comprehension to reverse a list in Python. Let’s understand this with the help of the below program.

def reverseList(inputList):

   reversedList = [inputList[i] for i in range(len(inputList)-1, -1, -1)]

   return reversedList

inputList = ["Joanna", "Chandler", "Monica", "Joey", "Jessica", "Sarah", "Phoebe", "Rose", "Rachel"]

print("The original list is - ", inputList)

print("The list after reversal is - ", reverseList(inputList))

In the above program, you saw the use of list comprehension to reverse a list. The range of the list consists of three values. The first value is the starting value which is the ending index of the list. The second value is the index just preceding the starting index because a for loop traverses up to an element just before the ending index i.e, excluding the stop index.  The step value is -1 which means that the traversal has to be done in reverse order. Let’s verify the output.

ReversingList_5

List comprehension is better than the for loop technique, however, it’s still inefficient as it creates a new list and contains more computation than any other method.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Wrapping Up!

To conclude, in this comprehensive guide, you looked into what lists in Python are, and the different ways through which you can reverse a list in Python. This article discussed techniques such as reversing a list using the built-in reverse() and reversed() methods, list slicing, for loops, and list comprehensions. You also got to know which ones are efficient and can be used compared to others. 

We hope that this article on reversing a list in Python will equip you with multiple techniques and find out the best ones for yourself. If you have any questions for us, leave them in the comments section of this article. Our experts will get back to you as soon as possible!

Happy Learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.