In this article, we will learn 5 ways to compare Tuples in Python with code examples. In this article, we will use different ways to do tuple item comparisons like by using default logical operations, comparison by using functions, etc.
1. Basic tuple Comparisons
In the basic tuple comparison, we will check two tuples for equal, unequal, and greater
1.Equal
Tuples are compared position by position means the first item of a tuple compared with the first item of the second tuple and so on. If all the items will equal then the result will be true.
tuple_a= (1,2,3,4,5,6)
tuple_b = (1,2,3,4,5,6)
#comparing tuple are equal
print('Tuples are equal =',tuple_a==tuple_b)
Output:
Tuples are equal = True
2. Unequal
In this code example all the elements of tuple_a and tuple_b compare using the not equal operator(!=).If the result is TRUE then tuples are not equal else both are equal.
tuple_a= (1,2,3,4,5,8)
tuple_b = (1,2,3,4,5,6)
#comparing tuple are equal
print('all elements of first tuple are not equal second =',tuple_a!= tuple_b)
Output:
all elements of first tuple are not equal second = True
3.Greater
tuple_a= (1,2,3,4,5,6)
tuple_b = (1,2,3,4,5,7)
#checking tuple are equal
print('Elements of first tuple greater than second =',tuple_a > tuple_b)
Output:
Elements of first tuple greater than second = False
2. All elements of first tuple less than second tuple
In this example, we will learn how to check for less than for all elements of one tuple to another, Let us understand with Example:
By using all(), map() and lambda functions.
we are comparing tuples using all, map(), and lambda function. We are setting up conditions using lambda, we are returning the tuple element which passed the condition.
tuple= (1,2,3,4,5,6)
tuple1 = (7,8,9,10,11,12)
#checking all items of tuple
tuples_compare = all(map(lambda k, l: k < l, tuple, tuple1))
print(' Are all elements of first tuple less than second \n =',tuples_compare)
Output:
Are all elements of first tuple less than second
= True
3. Compare for similar items in two tuples
Here we will learn how to compare two tuples for the same values by using a python set.
Using Set()
In this code example, we are using a set with & operator to compare two tuples that are intersection operations on two sets. It is showing the result of common elements in both tuples.
- Firstly, we have converted the tuple into the set.
- Secondly, We have converted tuple1 into a set
- Finally, find the intersection of both tuples to find common elements
tuple= (1,2,3,4,5,6)
tuple1 = (1,8,6,10,11,12)
#common items in both tuple
common_elements = set(tuple) & set(tuple1)
print('elements exist in both tuple \n =',common_elements)
Output:
elements exist in both tuple
= {1, 6}
4. Compare the heterogeneous items using map()
To compare heterogeneous datatypes tuples, where tuples have different data type elements first, we will need to convert them into the same datatype elements before we compare them. More, We will understand with an example
Some important points to note before we jump to our examples:
- == operator behave differently on heterogeneous datatypes.
- < and > operators work the same with the heterogeneous datatype.
tupleint= (1,2,3,4,5,6)
tuplestr = (7,8,9,10,"11","12")
#same tuples
t1 = (1,"2",3,4,5,6)
t2 = (1,2,3,4,5,6)
#map() to convert all element to same dataype before we compare for less than and equal
lessthan_compare = tuple(map(int, tuplestr)) < tupleint
equal_compare = tuple(map(int, t1)) == t2
#tuples equal compare with or without map()
print('equal on heterogeneous datatype without map() =',t1 == t2)
print('equal on heterogeneous datatype with map()=',equal_compare)
#tuples less than compare with or without map()
print('lessthan without map() =',tuplestr<tupleint)
print('lessthan with map() =',lessthan_compare)
Output:
equal on heterogeneous datatype without map() = False
equal on heterogeneous datatype with map() = True
lessthan without map() = False
lessthan with map() = False
Summary
In this post we have learned 5 ways to Compare Tuples in Python using set map() lambda,all() function