Python | Check if two list of tuples are identical
Last Updated :
23 Apr, 2023
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.
Python3
test_list1 = [( 10 , 4 ), ( 2 , 5 )]
test_list2 = [( 10 , 4 ), ( 2 , 5 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = test_list1 = = test_list2
print ( "Are tuple lists identical ? : " + str (res))
|
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.
Python
test_list1 = [( 10 , 4 ), ( 2 , 5 )]
test_list2 = [( 10 , 4 ), ( 2 , 5 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = not cmp (test_list1, test_list2)
print ( "Are tuple lists identical ? : " + str (res))
|
Output
The original list 1 : [(10, 4), (2, 5)]
The original list 2 : [(10, 4), (2, 5)]
Are tuple lists identical ? : True
Method #3 : Using all() and zip()
This method uses the built-in function ‘zip’ to iterate through the two lists of tuples and compares the elements with the use of the “all()” function. The all() function returns true if all elements in an iterable are true, and in this case, it checks if all the elements in the zipped tuple are equal, hence returning true if the lists are identical, and false otherwise.
Python3
test_list1 = [( 10 , 4 ), ( 2 , 5 )]
test_list2 = [( 10 , 4 ), ( 2 , 5 )]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = all (x = = y for x, y in zip (test_list1, test_list2))
print ( "Are tuple lists identical ? : " + str (res))
|
Output
The original list 1 : [(10, 4), (2, 5)]
The original list 2 : [(10, 4), (2, 5)]
Are tuple lists identical ? : True
Time Complexity: O(n) where n is the length of the tuples
Auxiliary Space: O(1)
Method 4 : using the set() function
- Create two lists of tuples test_list1 and test_list2.
- Convert both the lists to sets set1 and set2 respectively.
- Check if set1 is equal to set2 using the == operator.
- If set1 is equal to set2, it means that the two original lists are identical. Set the result res to True, otherwise set it to False.
- Print the result res along with an appropriate message.
Python3
test_list1 = [( 10 , 4 ), ( 2 , 5 )]
test_list2 = [( 10 , 4 ), ( 2 , 5 )]
set1 = set (test_list1)
set2 = set (test_list2)
res = set1 = = set2
print ( "Are tuple lists identical? : " + str (res))
|
Output
Are tuple lists identical? : True
Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(n), where n is the length of the lists (to store the sets).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...