Open In App

Python – How to Check if two lists are reverse equal

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to check if two lists are reverse of each other. This kind of problem can have application in many domains such as day-day programming and school programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list1 = [5, 6, 7], test_list2 = [7, 6, 5] 
Output : True 

Input : test_list1 = [5, 6], test_list2 = [7, 6] 
Output : False

Method #1 : Using reversed() and “==” operator The combination of above functions can be used to solve this problem. In this, we perform the task of reversing using reversed() and testing for equality using “==” operator. 

Python3




# Python3 code to demonstrate working of
# Check if two lists are reverse equal
# Using reversed() + == operator
 
# initializing lists
test_list1 = [5, 6, 7, 8]
test_list2 = [8, 7, 6, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Check if two lists are reverse equal
# Using reversed() + == operator
res = test_list1 == list(reversed(test_list2))
 
# printing result
print("Are both list reverse of each other ? : " + str(res))


Output : 

The original list 1 : [5, 6, 7, 8]
The original list 2 : [8, 7, 6, 5]
Are both list reverse of each other ? : True

Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(n), where n is the length of the reversed list created using reversed() function. This reversed list is stored in memory temporarily and occupies space proportional to the length of the input list. However, the space used by the input lists themselves is not included in the auxiliary space complexity since they are already given and not created by the program.

Method #2: Using list slicing + “==” operator This is yet another way to solve this problem. In this, we perform the task of list reversing using slice technique. 

Python3




# Python3 code to demonstrate working of
# Check if two lists are reverse equal
# Using list slicing + "==" operator
 
# initializing lists
test_list1 = [5, 6, 7, 8]
test_list2 = [8, 7, 6, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Check if two lists are reverse equal
# Using list slicing + "==" operator
res = test_list1 == test_list2[::-1]
 
# printing result
print("Are both list reverse of each other ? : " + str(res))


Output : 

The original list 1 : [5, 6, 7, 8]
The original list 2 : [8, 7, 6, 5]
Are both list reverse of each other ? : True

Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(n)

Method #3: Using insert function

Python3




# Python code to check if twi
# lists are reverse equal or not
 
lst1 = [5, 6, 7, 8]; lst2 = [8, 7, 6, 5]
l = []
# reversing list 2 elements
# using insert function
for i in lst2:
    l.insert(0, i)
# comparing the first list with
# reverse list if both are equal
# then return true otherwise false
if lst1 == l:
    print("True")
else:
    print("False")
 
    # this code is contributed by gangarajula laxmi


Output

True

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  insert function performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #4: Using a loop to compare elements

Compares the elements of both lists using a loop. We can compare the first element of the first list with the last element of the second list, the second element of the first list with the second last element of the second list, and so on until we reach the middle of both lists.

Python3




# Python3 code to demonstrate working of
# Check if two lists are reverse equal
# Using a loop to compare elements
 
# initializing lists
test_list1 = [5, 6, 7, 8]
test_list2 = [8, 7, 6, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Check if two lists are reverse equal
# Using a loop to compare elements
n = len(test_list1)
res = True
for i in range(n):
    if test_list1[i] != test_list2[n-1-i]:
        res = False
        break
 
# printing result
print("Are both list reverse of each other ? : " + str(res))


Output

The original list 1 : [5, 6, 7, 8]
The original list 2 : [8, 7, 6, 5]
Are both list reverse of each other ? : True

Time complexity: O(n), where n is the length of the lists. This is because we need to compare each element of the lists once.
Auxiliary space: O(1), because we are only using a constant amount of extra space to store variables.

Method #5: Using recursion

  • Define a function “is_reverse_equal” that takes in two lists as arguments.
  • Base case: If the length of the lists is 0, return True.
  • Recursive case: Check if the first element of the first list matches the last element of the second list.
  • If the elements match, recursively call the function with the first list sliced from index 1 to the end and the second list sliced from index 0 to the second to last element.
  • If the elements do not match, return False.

Python3




# Python3 code to demonstrate working of
# Check if two lists are reverse equal
# Using recursion
 
# defining a function for checking reverse equality
def is_reverse_equal(list1, list2):
    # base case: if the lists are empty
    if len(list1) == 0 and len(list2) == 0:
        return True
    # recursive case: if the first element of list1 matches the last element of list2
    elif list1[0] == list2[-1]:
        # recursively call the function with sliced lists
        return is_reverse_equal(list1[1:], list2[:-1])
    # if the elements do not match
    else:
        return False
 
# initializing lists
test_list1 = [5, 6, 7, 8]
test_list2 = [8, 7, 6, 5]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Check if two lists are reverse equal
# Using recursion
res = is_reverse_equal(test_list1, test_list2)
 
# printing result
print("Are both list reverse of each other ? : " + str(res))


Output

The original list 1 : [5, 6, 7, 8]
The original list 2 : [8, 7, 6, 5]
Are both list reverse of each other ? : True

Time complexity: O(n)
Auxiliary space: O(n) – due to the recursive calls on the function call stack.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads