Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Compare tuples

Python | Compare tuples

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




# Python3 code to demonstrate working of
# Comparing tuples
# using generator expression + all() + zip()
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (13, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Comparing tuples
# using generator expression + all() + zip()
res = all(x < y for x, y in zip(test_tup1, test_tup2))
 
# printing result
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




# Python3 code to demonstrate working of
# Comparing tuples
# using all() + lambda + map()
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (13, 5, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Comparing tuples
# using all() + lambda + map()
res = all(map(lambda i, j: i < j, test_tup1, test_tup2))
 
# printing result
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
 
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (13, 1, 18)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Comparing tuples
# using numpy.greater()
res = all(np.greater(test_tup2, test_tup1))
 
# printing result
print("Are all elements in second tuple greater than first ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




#initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (13, 5, 18)
#printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
#comparing elements in the tuples
res = False
if test_tup2 > test_tup1:
    res = True
# printing result
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)
#printing original tuples
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)
#This code is contributed by Vinay Pinjala.


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)

RELATED ARTICLES

Most Popular

Recent Comments