Python | Reverse a list without recursion

Let us take a look at how to solve this problem with various different methods.

Method 1: Using reduce() of functools method

from functools import reduce

lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 

print('List entered by the user is',lst)
new_list = reduce(lambda a, b : [b]+[a] if type(a)==int else [b]+a , lst)
print('Reverse of the entered list :',new_list)

Output:

Enter number of elements : 7
11
12
13
14
15
16
17
List entered by the user is [11, 12, 13, 14, 15, 16, 17]
Reverse of the entered list : [17, 16, 15, 14, 13, 12, 11]

Explanation:

At first, we took the input from the user, now we used the reduce() function of functools. The main responsibility of reduce() function is applying a function to all of the list components indicated in the sequence sent along is what the reduce(fun,seq) function does. The array is finally in reverse order after the list iterates through each entry and adds the element to the top of the list. Each time we iterate, we transform the item into a list and combine the front element of the prior array with the current element list.

Method 2: Using the concept of string slicing

def Reverse(lst):
    new_lst = lst[::-1]
    return new_lst
 
 
lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 

print('Reverse of the entered list :',Reverse(lst))

Explanation

We used the concept of negative indexing. We passed on our input to the function. With the help of this (:) operator, we can define the step as well as the starting and ending points for the slicing. List slicing takes an existing list and creates a new list.

Method 3: Using the pointers

def Reverse(lst):
    left = 0
    right = len(lst)-1
    while (left < right):
        temp = lst[left]
        lst[left] = lst[right]
        lst[right] = temp
        left += 1
        right -= 1
 
    return lst
 
 
lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 
print('The list entered by user :',lst)
print('Reverse of the entered list :',Reverse(lst))

We shall declare two pointers in this procedure, which are essentially the start index and the end index, let ‘left’ and ‘right’. We shall alternate the entries at indexes “left” and “right” while scanning the list. The “left” pointer will advance, while the “right” pointer will revert to its previous position. Up to “first” and “last,” the process will be carried out. Both an even number of components and an odd number of elements can be used with this.

Method 4: With the help of reverse()

lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 

lst.reverse()
print('The list entered by user :',lst)
print('Reverse of the entered list :',lst)

Method 4: With the help of reversed()

lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 


print('The list entered by user :',lst)
print('Reverse of the entered list :',list(reversed(lst)))

Method 5: Using the swapping concept

def Reverse(lst,no_of_elements):

	if(no_of_elements==1):
		return lst
	
	elif(no_of_elements==2):
		lst[0],lst[1],=lst[1],lst[0]
		return lst
	
	else:
		i=0
		while(i<no_of_elements//2):

			lst[i],lst[no_of_elements-i-1]=lst[no_of_elements-i-1],lst[i]
	
			if((i!=i+1 and no_of_elements-i-1 != no_of_elements-i-2) and (i!=no_of_elements-i-2 and no_of_elements-i-1!=i+1)):
				lst[i+1],lst[no_of_elements-i-2]=lst[no_of_elements-i-2],lst[i+1]
			i+=2
		return lst
 
lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 


print('The list entered by user :',lst)
print('Reverse of the entered list :',Reverse(lst,n))

Explanation:

Here we used multiple conditionals, the first if statement is to check if there is only one element in the list then we simply have to return that only. If there are two elements then we will simply use the concept of swapping by using a third temporary variable. But when the number of elements is more than three, then we will move on to the third conditional in the function.

The first present and last present numbers in the list were switched after initialising i=0 and using the for loop for I in size/2. Now that we’ve used conditionals, we said that if the first and next number’s indices are different, swap the second and third subsequent numbers and increase i+=2. Upon completion of the loop, we returned our lst.

Method 6: Using the concept of list comprehension and append()

lst = []
n = int(input("Enter number of elements : "))
  
for i in range(0, n):
    ele = int(input())
    lst.append(ele) 


print('The list entered by user :',lst)
list_comp = [lst[len(lst) - i]
            for i in range(1, len(lst)+1)]
print('Reverse of the entered list :',list_comp)

Explanation:

Once the list is taken as input from the user, we defined a new variable. This method does not arrange the list in order. The original array need not be duplicated. To invert the array and return the list, we employ list comprehension. By using the range, we cycle across the array after determining its length. We now subtract the length of the original list by the iterator’s index in order to replace the final entry with the first.

Here is the end of our article. Among some of the methods, we saw the simplest one is to use reverse() and reversed() functions.

Share:

Author: Ayush Purawr