Python provides us with various ways of reversing a list. We will go through some of the many techniques on how a list in Python can be reversed.
Example:
Input: list = [4, 5, 6, 7, 8, 9] Output: [9, 8, 7, 6, 5, 4] Explanation: The list we are having in the output is reversed to the list we have in the input.
Reversing a List in Python
Below are the approaches that we will cover in this article:
- Using the slicing technique
- Reversing list by swapping present and last numbers at a time
- Using the reversed() and reverse() built-in function
- Using a two-pointer approach
- Using the insert() function
- Using list comprehension
- Reversing a list using Numpy
Reverse Array using the slicing
In this technique, a copy of the list is made, and the list is not sorted in place. Creating a copy requires more space to hold all the existing elements. This exhausts more memory. Here we are using the slicing technique to reverse our list in Python.
Python3
# Reversing a list using slicing technique def Reverse(lst): new_lst = lst[:: - 1 ] return new_lst lst = [ 10 , 11 , 12 , 13 , 14 , 15 ] print (Reverse(lst)) |
[15, 14, 13, 12, 11, 10]
Time complexity: O(n)
Auxiliary space: O(n)
Reverse list by swapping present and last numbers at a Time
Here is the approach:
If the arr[], size if the length of the array is 1, then return arr. elif length of the array is 2, swap the first and last number and return arr. otherwise, initialize i=0. Loop for i in size//2 then swap the first present and last present numbers if the first and next numbers indexes are not same, then swap next and last of next numbers then increment i+=2, and after looping return arr.
Python3
#Python program to reverse an array def list_reverse(arr,size): #if only one element present, then return the array if (size = = 1 ): return arr #if only two elements present, then swap both the numbers. elif (size = = 2 ): arr[ 0 ],arr[ 1 ], = arr[ 1 ],arr[ 0 ] return arr #if more than two elements presents, then swap first and last numbers. else : i = 0 while (i<size / / 2 ): #swap present and preceding numbers at time and jump to second element after swap arr[i],arr[size - i - 1 ] = arr[size - i - 1 ],arr[i] #skip if present and preceding numbers indexes are same if ((i! = i + 1 and size - i - 1 ! = size - i - 2 ) and (i! = size - i - 2 and size - i - 1 ! = i + 1 )): arr[i + 1 ],arr[size - i - 2 ] = arr[size - i - 2 ],arr[i + 1 ] i + = 2 return arr arr = [ 1 , 2 , 3 , 4 , 5 ] size = 5 print ( 'Original list: ' ,arr) print ( "Reversed list: " ,list_reverse(arr,size)) #This contributed by SR.Dhanush |
Original list: [1, 2, 3, 4, 5] Reversed list: [5, 4, 3, 2, 1]
Time Complexity: O(log2(n)), where n is the length of the given array.
Auxiliary Space: O(1)
Reverse Array using the reversed() and reverse() built-in function
Using reversed() we can reverse the list and a list_reverseiterator object is created, from which we can create a list using list() type casting. Or, we can also use the list reverse() function to reverse list in place.
Python3
lst = [ 10 , 11 , 12 , 13 , 14 , 15 ] lst.reverse() print ( "Using reverse() " , lst) print ( "Using reversed() " , list ( reversed (lst))) |
Using reverse() [15, 14, 13, 12, 11, 10] Using reversed() [10, 11, 12, 13, 14, 15]
Time complexity: O(n), where n is the length of the list lst.
Auxiliary space: O(1) since it modifies the original list in place and does not create a new list.
Reverse a list using a two-pointer approach
In this method, we will declare two pointers(basically the start index and the end index, let ‘left’ and ‘right’). While scanning the list, in each iteration we will swap the elements at index ‘left’ and ‘right’. The ‘left’ pointer will move forward and the ‘right’ pointer will move backward. We will continue the process till ‘first’ < ‘last’. This will work for both an even number of elements as well an odd number of elements.
Python3
# Reversing a list using two-pointer approach def reverse_list(arr): left = 0 right = len (arr) - 1 while (left < right): # Swap temp = arr[left] arr[left] = arr[right] arr[right] = temp left + = 1 right - = 1 return arr arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] print (reverse_list(arr)) |
[7, 6, 5, 4, 3, 2, 1]
Time Complexity: O(N)
Auxiliary Space: O(1)
Reverse a list using the insert() function
In this method, we neither reverse a list in place (modify the original list) nor create any copy of the list. Instead, we keep inserting items at the 0th index of the list, this will automatically reverse the list.
Python3
# input list lst = [ 10 , 11 , 12 , 13 , 14 , 15 ] # the above input can also be given as # lst=list(map(int,input().split())) l = [] # empty list # iterate to reverse the list for i in lst: # reversing the list l.insert( 0 , i) # printing result print (l) |
[15, 14, 13, 12, 11, 10]
Time complexity: O(n)
Auxiliary Space: O(n), where n is the length of the list.
Reverse a list using list comprehension
In this technique, the list is not sorted in place. A copy of the original array is not required. We use list comprehension to reverse the array and return the list.
We find the length of the array and then iterate over it using the range. Now, to replace the last element with the first, we subtract the length of the original list from the index of the iterator.
Python3
original_list = [ 10 , 11 , 12 , 13 , 14 , 15 ] new_list = [original_list[ len (original_list) - i] for i in range ( 1 , len (original_list) + 1 )] print (new_list) |
[15, 14, 13, 12, 11, 10]
Time complexity: O(n), where n is the length of the original_list.
Auxiliary space: O(n),
Reverse a list using Numpy
Here we are going to use numpy package:
Initialize the input list my_listConvert my_list to a 1D numpy array using np.array(my_list)Reverse the order of the array using my_array[::-1]Convert the reversed numpy array back to a list using .tolist()
Print the reversed list
Python3
import numpy as np # Input list my_list = [ 4 , 5 , 6 , 7 , 8 , 9 ] # Convert the list to a 1D numpy array my_array = np.array(my_list) # Reverse the order of the array reversed_array = my_array[:: - 1 ] # Convert the reversed array to a list reversed_list = reversed_array.tolist() # Print the reversed list print (reversed_list) |
Output:
[9, 8, 7, 6, 5, 4]
Time complexity: O(n)
Auxiliary space: O(n)