5 ways to Compare Tuples in Python

Tuple

In this article we will learn 5 ways to compare Tuples in Python with code example.

In this article we will use different ways to do tuple item comparisons like by using default logical operations, comparison by using functions etc.

Basic tuple Comparisons

1.Equal

Tuples are compared position by position means the first item of a tuple compare 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 the first tuple less than the second tuple

In this example, we will learn how to check for less than for all elements of one tuple to another,

By using all(), map() and lambda functions.

Let us understand with Example:

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.

Using Set()

In this code example we are using set with & operator to compare two tuples.It is showing the result of common elements in both tuples.

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.

Some important points to note before we jump to our examples:

  1. == operator behave differently on heterogeneous datatypes.

2. < and > operators work same with heterogeneous datatype.

More,We will understand with an example

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