Python - How to Check if two lists are reverse equal
Last Updated :
16 May, 2023
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
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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python | Check if two list of tuples are identical
Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read
How to compare two lists in Python?
In Python, there might be a situation where you might need to compare two lists which means checking if the lists are of the same length and if the elements of the lists are equal or not. Let us explore this with a simple example of comparing two lists.Pythona = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] #
3 min read
Python - Check if Splits are equal
Given String separated by delim, check if all splits are similar. Input : '45#45#45', delim = '#' Output : True Explanation : All equal to 45. Input : '4@5@5', delim = '@' Output : False Explanation : 1st segment equal to 4, rest 5. Method #1 : Using set() + len() + split() In this, we perform split
2 min read
Python Program To Check If Two Linked Lists Are Identical
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE
4 min read
How to Check if an Index Exists in Python Lists
When working with lists in Python, sometimes we need to check if a particular index exists. This is important because if we try to access an index that is out of range, we will get an error. Let's look at some simple ways to check if an index exists in a Python list.The easiest methods to check if g
2 min read
How To Reverse A List In Python Using Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. In this article, we will see how we can reverse a list using slicing in Python. Reverse a List Using Slicing in PythonBelow are some of the examples by which we can perform rev
3 min read
Python | Reverse each tuple in a list of tuples
Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega
5 min read
Python | Check if tuple and list are identical
Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is th
7 min read
Check if Two Lists Have Same Elements regardless of Order - Python
We have multiple ways to check if two lists have the same elements, regardless of their order. The simplest way to check if two lists have the same elements is to sort both lists and then compare them. If both lists have the same elements in the same order after sorting, they are the same.Pythondef
2 min read
Python - Check if all elements in List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read