Python has four built-in data types that are used to store collections of data. These Python data types are:
A tuple in Python is a collection of data items or objects stored inside a single variable. The data items inside a tuple are separated by commas. Syntactically, tuples store data inside round brackets i.e. (<insert elements separated by commas>).
Tuples in Python are immutable, meaning that once a tuple is created, it cannot be changed. It is unchangeable. Also, tuples are an ordered collection of data.
This implies that tuples are 0-indexed and the data items inside a tuple can be directly accessed using their index. It also supports negative indexing, just like lists and strings in Python. Tuples in Python can hold elements of the same data type or of different types as well. That is, tuples can be both homogenous and heterogeneous.
Below are some examples of tuples in Python:
tuple1 = (45, 2, 67, 7) tuple2 = ('cat', 'fish', 'dog') tuple3 = (52.5, 36.4, 0.2) tuple4 = (4, 8.6, 'hey')
In this tutorial, you will learn how tuples are compared in Python and also go through some examples regarding the same.
Tuple Comparison in Python
In Python, a tuple comparison is a method for comparing two or more tuples based on their elements and establishing their relative order.
Tuples are compared lexicographically. This implies that the corresponding elements of the tuples under consideration are compared to find the relative order. Hence, the comparison is performed elementwise i.e., the first element from the first tuple is compared with the first element from the second tuple and so on.
Sometimes the first comparison is enough to conclude the result but if it is not then the tuples are compared further until all the elements are compared or we get a result.
Tuples can be compared using comparison operators like >, >=, <, <=, == and !=.
Important points to note
- While comparing two data collections in Python, they should be of the same type. Comparing a list with a tuple will result in an error.
- For two collections to be equal, they must be of the same data type, should have the same length and each pair of corresponding elements must be equal.
- Tuple comparison in Python is lexicographical. A tuple being ‘greater than‘ another means it appears after the other. Similarly, a tuple is ‘smaller than‘ another implies that is appears before the other.
- A tuple is ‘greater than’ another if an element in it is greater than the correspoding element the other tuple. Similar rule can be deduced for the other comparison operators.
Performing comparison of tuples using Python
Have a look at the below code:
a = (5, 6, 10) b = (4, 12, 7) print('Tuple a:', a, '\nTuple b:', b) print('\nTuple a > Tuple b?:', a>b) print('\nTuple a < Tuple b?:', a<b) print('\nTuple a == Tuple b?:', a==b)
Tuple a: (5, 6, 10) Tuple b: (4, 12, 7) Tuple a > Tuple b?: True Tuple a < Tuple b?: False Tuple a == Tuple b?: False
This example contains two tuples a and b. These tuples are compared as follows:
- Greater than: When comparing the two tuples, the first elements in both the tuples are compared first. Since 5>4, it is established that tuple a is greater than tuples b and the result is True. No further comparisons are made as it is not needed.
- Smaller than: Comparing the first elements, 5 is not smaller than 4, so the result is False that is, tuple a is not smaller than tuple b.
- Equal: Since 5 is not equal to 4, tuple a is not equal to tuple b. Hence the result is False.
This is how tuple comparison works in Python.
Using NumPy to compare two tuples in Python
The Python library NumPy can also be utilised to compare tuples in Python instead of using the comparison operators. NumPy supports function like
equal() to compare two collections of objects, elementwise. These functions can be used to perform comparison of tuples as demonstrated in the below example:
import numpy as np a = (2, 10, 3) b = (15, 10, 33) print('Tuple a:', a, '\nTuple b:', b) print('\nElement-wise Comparison of the tuples using greater():', np.greater(a, b)) print('\nElement-wise Comparison of the tuples using less():', np.less(a, b)) print('\nElement-wise Comparison of the tuples using equal():', np.equal(a, b))
Tuple a: (2, 10, 3) Tuple b: (15, 10, 33) Element-wise Comparison of the tuples using greater(): [False False False] Element-wise Comparison of the tuples using less(): [ True False True] Element-wise Comparison of the tuples using equal(): [False True False]
- greater(): This function compares the corresponding pairs of elements. Since 2 is not greater than 15, 10 is not greater than 10 and 3 is not greater than 33, the result of each element-wise comparison of the two tuples is False.
- less(): Here, 2<15 and 3<33 are True but as 10 is not less than 10, it is False.
- equal(): Since only 10==10, the result of this comparison is True and 2 is not equal to 15 and 3 is not equal to 33, the result of both the element-wise comparisons is False.
Tuple comparison by eliminating duplicates
If a tuple contains duplicates, you can eliminate them and then perform tuple comparison.
a = (1, 2, 5, 2, 3) b = (3, 2, 6, 1, 2) print('Tuple a:', a, '\nTuple b:', b) new_tuple_a = tuple(set(a)) new_tuple_b = tuple(set(b)) print('\nNew Tuple a:', new_tuple_a, '\nNew Tuple b:', new_tuple_b) print('\nTuple a > Tuple b?:', new_tuple_a>new_tuple_b) print('\nTuple a < Tuple b?:', new_tuple_a<new_tuple_b) print('\nTuple a == Tuple b?:', new_tuple_a==new_tuple_b)
Tuple a: (1, 2, 5, 2, 3) Tuple b: (3, 2, 6, 1, 2) New Tuple a: (1, 2, 3, 5) New Tuple b: (1, 2, 3, 6) Tuple a > Tuple b?: False Tuple a < Tuple b?: True Tuple a == Tuple b?: False
The duplicates in the tuples are eliminnated by first converting the tuples to another collection type called a set. The set only contains all the unique elements only once. Then these sets are type-casted back to tuple and the usual comparison is performed using the comparison operators.
Since the first 3 elements in both the new tuples are equal here, the result of the comparison is decided on the basis of the last pair of corresponding elements.
Comparing tuples with heterogenous elements
As discussed earlier in this tutorial, tuples can be heterogenous i.e. contain elements of different data types. Such tuples can also be compared.
a = (0, 2, 'a') b = (1, 5, 'd') print('Tuple a:', a, '\nTuple b:', b) print('\nTuple a > Tuple b?:', a>b) print('\nTuple a < Tuple b?:', a<b) print('\nTuple a == Tuple b?:', a==b)
Tuple a: (0, 2, 'a') Tuple b: (1, 5, 'd') Tuple a > Tuple b?: False Tuple a < Tuple b?: True Tuple a == Tuple b?: False
Since 0<1 and 2<5 is True and also the letter ‘a’ is lexicographically smaller than the letter ‘d’, the tuple a is smaller than tuple b.
In this tutorial you learnt how tuple comparison works in Python and also how to perform tuple comparison using Python and NumPy with the help of many examples.
Please visit askpython.com for more such easy-to-understand Python tutorials.