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 the simplest and elegant way to perform this task. It also checks for equality of tuple indices with one other.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Check if two list of tuples are identical
# using == operator
  
# initialize list of tuples 
test_list1 = [(10, 4), (2, 5)]
test_list2 = [(10, 4), (2, 5)]
  
# printing original tuples lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
  
# Check if two list of tuples are identical
# using == operator
res = test_list1 == test_list2
  
# printing result
print("Are tuple lists identical ? : " + str(res))

chevron_right


Output :



The original list 1 : [(10, 4), (2, 5)]
The original list 2 : [(10, 4), (2, 5)]
Are tuple lists identical ? : True

 

Method #2 : Using cmp()
This inbuilt function, computes the difference of values of tuples. If they are computed out to be 0, then it means that tuples are identical. Works only with Python2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate working of
# Check if two list of tuples are identical
# using cmp()
  
# initialize list of tuples 
test_list1 = [(10, 4), (2, 5)]
test_list2 = [(10, 4), (2, 5)]
  
# printing original tuples lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
  
# Check if two list of tuples are identical
# using cmp()
res = not cmp(test_list1, test_list2)
  
# printing result
print("Are tuple lists identical ? : " + str(res))

chevron_right


Output :

The original list 1 : [(10, 4), (2, 5)]
The original list 2 : [(10, 4), (2, 5)]
Are tuple lists identical ? : True


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.