How to Reverse a List in Python

In this Python tutorial, we will learn how to reverse a list in python. And we will cover different examples related to reversing a list. Here is the list of examples that we are going to cover.

  • Reverse a list in python with reverse function
  • Reverse a list in python with reversed iterator
  • Reverse a list in python without reverse function
  • How to reverse a list in python using while loop
  • Reverse a list in python using for loop
  • Reverse a list in python using slicing
  • Reverse a list in python using recursion

Reverse a list in python with reverse function

Python has an in-built function called reverse() that we can use to reverse a Python list. Let us see an example. We will create a list and reverse it using the reverse() function. have a look at the code below.

list1=[12,14,56,24,25,52,54]
list1.reverse()
print(list1)
  • We have created a list with the name list1 and used the reverse() fucntion to reverse the list. Look at the output below:
Reverse a list in python with reverse function
Reversed list

In this way, you can use the in-built reverse() function to reverse a list.

Read: Python program to reverse a string

Reverse a list in python with reversed iterator

There is another in-built option to reverse a list in python. We can use the reversed iterator. Using this option, we can iterate our list in reverse order or we can also store the results in the form of a list. Let us see how.

list1= [23, 54, 76, 82, 40, 45]
for element in reversed(list1):
    print(element)
Reverse a list in python with reverse function
Elements printed in reverse order

You can see that the elements are printed in reverse order.

Similarly, you can use this reversed iterator to make a list. But you have to convert this iterator object into a list.

list1= [23, 54, 76, 82, 40, 45]
print(list(reversed(list1)))
Reverse a list in python
Reversed List

In this way, you can use the reversed iterator to get a reversed list in python.

Read: 11 Python list methods

Reverse a list in python without reverse function

When we want to reverse a list in python, we generally use the reverse() function. But we can also write our own logic to complete the same task.

In the below sections, I have explained some of the methods that you can use to reverse a list in python without the reverse() function.

How to reverse a list in python using while loop

You can use the while loop to reverse a list in python. Look at the code below:

list1= [54,78,23,54,67,90,12,43]
print('Original list:', list1)
list2=[]
length=len(list1)
while length > 0:
    list2.append(list1[length-1])
    length = length - 1
print('Reversed list:', list2)
reverse a list in python using while loop
Reversed list
  • In the above code, we are itertating over the length of the list. We are decrementing the value of length and the loop will stop when the length is zero.
  • With the help of indexing, we are storing the values of the original list into a new list.

In this way, you can use the while loop to reverse a list in python.

Read: Python convert DataFrame to list

Reverse a list in python using for loop

You can also use the for loop to reverse a list in python. I will explain various examples where I will use the for loop to reverse a list.

Example 1:

Consider the following python code:

original_list=[71, 36, 51, 75, 82, 78, 40]
print('Orginal List:', original_list)
reversed_list=[]
for i in original_list:
    reversed_list.insert(0,i)
print('Reversed List:', reversed_list)
Reverse a list in python using for loop
Reversed List
  • In the above example, we are using the insert() function to insert an element at the first position of the new list.
  • When we insert an element at the first index in the list, the rest of the elements get shifted to the next index.
  • For example, the following are the values of the reversed list after every iteration:
    • [71]
    • [36, 71]
    • [51, 36, 71]
    • [75, 51, 36, 71]
    • [82, 75, 51, 36, 71]
    • [78, 82, 75, 51, 36, 71]
    • [40, 78, 82, 75, 51, 36, 71]
  • You can see that at the end of all the iterations, our list is reversed.

Now let us see another example of reversing a list using the for loop.

Example 2:

In this example, we will use the insert() function along with the pop() method of the list to reverse a list. The pop() method deletes the last element of the list and returns this deleted value that we can store into a variable.

This time we will not create a new list and reverse the elements in the existing list only.

original_list=[71, 36, 51, 75, 82, 78, 40]
print('Orginal List:', original_list)
for i in range(len(original_list)):
    popped= original_list.pop()
    original_list.insert(i, popped)
print('Reversed List:', original_list)
Reverse a list in python
Reversed List
  • We are using the pop() method to delete the last element and store this ddeleted element into a variable.
  • Then we are using the insert() fucntion to insert the popped out element in the list.
  • To understand it better, you can see the value of the list after every iteration:
    • [71, 36, 51, 75, 82, 78, 40]
    • [40, 71, 36, 51, 75, 82, 78]
    • [40, 78, 71, 36, 51, 75, 82]
    • [40, 78, 82, 71, 36, 51, 75]
    • [40, 78, 82, 75, 71, 36, 51]
    • [40, 78, 82, 75, 51, 71, 36]
    • [40, 78, 82, 75, 51, 36, 71]
    • [40, 78, 82, 75, 51, 36, 71]

Thus, you might have learned how you can use the pop() and the insert() function with for loop to reverse a list.

Example 3:

In this example, I will explain a very efficient way of reversing a list without the use of the inbuilt functions. Look at the code below;

list1 = [32, 65, 79, 45, 35, 62, 74]
print('Original List:', list1)
Length = len(list1)
for i in range(int(Length / 2)):
    temp = list1[i]
    list1[i] = list1[Length - i - 1]
    list1[Length - i - 1] = temp
print('Reversed List:', list1)
Reverse a list in python without reverse function
Reversed List
  • In the above code, the loop will run for half the number of elements in the list.
  • Ths means the time complexity for running the program will be less as compared to some other methods making it a very efficient soultion.

Explanation of code:

  • In the above program we are tyring to swap one element from the left side with the corresponding element from the right side inside a loop.
  • Using this logic, we will need to run this program only ahlf the number of total elements in the list.
  • Let me show you the iteration one by one:

Original List: [32, 65, 79, 45, 35, 62, 74]

Length of list: 7

Number of iterations: int(7/2) = 3

Iteration 1:

[74, 65, 79, 45, 35, 62, 32]

Iteration 2:

[74, 62, 79, 45, 35, 65, 32]

Iteration 3:

[74, 62, 35, 45, 79, 65, 32]

You can see that in the last iteration, we got our list reversed. In this way, you can use the for loop to reverse a list in python in a very efficient way.

Example 4:

The below example is just for practice and is not a feasible solution because of its high space and time complexity.

In this method, we are iterating over the original list elements. We are converting each element into a list and concatenating this with the new list which is initially empty.

original_list=[71, 36, 51, 75, 82, 78, 40]
print('Original List:', original_list) 
reversed_list=[]
for element in original_list:
    reversed_list= [element] + reversed_list
print('Reversed List:', reversed_list)
Reverse a list in python
Reversed List

Read: Python string to list

Reverse a list in python using slicing

In this section, you will learn how you can use the slicing method to reverse a list. In this method, we have to make a new copy of the list. This increases the space requirements of a program.

Have a look at the code below:

list1=[44, 75, 24, 78, 24, 84, 24]
print('Original List', list1)
new_list=list1[::-1]
print('Reversed List', new_list)
Reverse a list in python using slicing
Reversed List

You can see that a copy of the list is storing the elements in reverse order. In this way, you can use the slicing or indexing method to get a list in reversed order.

Read: Python creates a dictionary from two lists

Reverse a list in python using recursion

You can also use some recursion methods to reverse a list in python. Let us understand with the help of an example.

list1= [43, 56, 23, 75, 92, 34, 45]
print('Original list:', list1)
def rev(lst):
    if len(lst) == 0:
        return []
    return [lst[-1]] + rev(lst[:-1])
print('Reversed list:', rev(list1))
Reverse a list in python using recursion
Reversing a list using recursion
  • In the above code we have created a recursive function.
  • This function returns the last element of the list and converts it into a list.
  • Then this function uses the concatenation operator and calls itself again but this time the list is passed excluding the last element of the list.
  • As a result, the function will return all the elements from the last position to the first position recursively and concatenate them at the last iteration.
  • This will return a single list that will be in the reverse order.

Let me explain this visually:

Original List: rev([43, 56, 23, 75, 92, 34, 45])

Iteration 1:

Now this function will break into:

[45] + rev([43, 56, 23, 75, 92, 34])

Iteration 2:

Similarly, the function will again break into:

[45] + [34] + rev([43, 56, 23, 75, 92])

Iteration 3:

[45] + [34] + [92] + rev([43, 56, 23, 75])

Iteration 4:

[45] + [34] + [92] + [75] + rev([43, 56, 23])

Iteration 5:

[45] + [34] + [92] + [75] + [23] + rev([43, 56])

Iteration 6:

[45] + [34] + [92] + [75] + [23] + [56] + rev([43])

which is equivalent to:

[45] + [34] + [92] + [75] + [23] + [56] + [43] = [45, 34, 92, 75, 23, 56, 43]

Hence at the last iteration, we got our list reversed. In this way, you can use the recursion method to reverse a list in python.

You may also like to read the following python tutorials.

Thus, you might have learned the various methods to reverse a list in python. the following is the list of methods that we discussed above.

  • Reverse a list in python with reverse function
  • Reverse a list in python with reversed iterator
  • Reverse a list in python without reverse function
  • How to reverse a list in python using while loop
  • Reverse a list in python using for loop
  • Reverse a list in python using slicing
  • Reverse a list in python using recursion