Skip to content
Related Articles

Related Articles

Python – How to Check if two lists are reverse equal
  • Last Updated : 03 Jul, 2020

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 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

 

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 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :