Python | Compare tuples
Last Updated :
16 Feb, 2023
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it’s corresponding index in other tuple. This can have many possible applications. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using all() + generator expression + zip() The combination of above functionalities can be used to perform this task. In this, we just compare all elements using all(). The cross tuple access is done by zip() and generator expression gives us the logic to compare.
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 13 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = all (x < y for x, y in zip (test_tup1, test_tup2))
print ( "Are all elements in second tuple greater than first ? : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (13, 5, 18)
Are all elements in second tuple greater than first ? : True
Method #2 : Using all() + map() + lambda The combination of above functionalities can be used to perform this particular task. In this, we perform extension of logic to each element using map() and logic generation by lambda function.
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 13 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = all ( map ( lambda i, j: i < j, test_tup1, test_tup2))
print ( "Are all elements in second tuple greater than first ? : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (13, 5, 18)
Are all elements in second tuple greater than first ? : True
Using Numpy:
Note: Install numpy module using command “pip install numpy”
Using numpy library, one can use the numpy.greater() function to compare the elements of two tuples and return an array of boolean values indicating whether each element in the first tuple is greater than the corresponding element in the second tuple.
Python3
import numpy as np
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 13 , 1 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = all (np.greater(test_tup2, test_tup1))
print ( "Are all elements in second tuple greater than first ? : " + str (res))
|
Output:
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (13, 1, 18)
Are all elements in the second tuple greater than first ? : False
Time and Auxiliary space, which is O(n) where n is the number of elements in the tuple.
Method #2 : Using tuple():
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 13 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = False
if test_tup2 > test_tup1:
res = True
print ( "Are all elements in second tuple greater than first ? : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (13, 5, 18)
Are all elements in second tuple greater than first ? : True
Time complexity: O(N)
Auxiliary Space: O(1)
Example #3: Using all() and zip()
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 13 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = all (x > y for x, y in zip (test_tup2, test_tup1))
print ( "Are all elements in second tuple greater than first ? :" , res)
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (13, 5, 18)
Are all elements in second tuple greater than first ? : True
Time complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...