Python compares tuples lexicographically, which means it compares the first elements, and only when they are equal does it move on to the second elements, and so on, the same way a printed dictionary orders words letter by letter. That rule makes tuples handy for sorting by several fields and for comparing version numbers.
v1 = (3, 12, 4)
v2 = (3, 13, 0)
older = v1 < v2 # True, decided by 12 < 13
equal = v1 == (3, 12, 4) # True
We go through the comparison rules, tuples of different lengths, mixed types, element-wise checks with all(), and sorting by tuple keys. Results are from Python 3.14.
1. How Tuple Comparison Works
Python walks both tuples from the left and looks for the first position where the elements differ.
- If it finds such a position, the result of comparing those two elements is the result for the whole tuples.
- If one tuple runs out first, the shorter tuple is the smaller one.
- If both run out at the same time, the tuples are equal.
a = (1, 2, 3) < (1, 2, 4) # True, the third elements decide
b = (1, 9, 9) < (2, 0, 0) # True, the first elements decide
c = (1, 2) < (1, 2, 0) # True, the shorter tuple is smaller
d = (1,) == (1.0,) # True, 1 == 1.0
Elements after the first difference are never looked at, so (1, 9, 9) < (2, 0, 0) is True even though 9 is bigger than 0.
2. Mixed Types and TypeError
Equality works between any types, but < and > raise TypeError when Python reaches a pair it cannot order, such as a number and a string. Because the comparison stops at the first difference, a pair after it is never checked.
same = (1, 2, 3) == (1, 2, "3") # False, no error
early = (1, "a") < (2, 2) # True, decided before the string is reached
bad = (1, "a") < (1, 2) # TypeError: '<' not supported between instances of 'str' and 'int'
When the data can contain strings that hold numbers, such as values read from a CSV file, we convert them first, for example with tuple(map(int, row)).
3. Element-wise Comparison with all() and zip()
Lexicographic order answers “which tuple comes first”. To ask whether every element of one tuple is smaller than the matching element of another, we combine all() and zip(); the two questions often have different answers.
t1, t2 = (1, 5), (2, 0)
lexical = t1 < t2 # True
every = all(x < y for x, y in zip(t1, t2)) # False, 5 is not < 0
Note that zip() stops at the shorter tuple, so we check the lengths separately when they can differ, or pass strict=True to get a ValueError on a length mismatch.
4. Sorting by Several Fields
Because tuples compare field by field, a key function that returns a tuple sorts by the first field and uses the second one for ties. A minus sign on a number sorts that field in descending order.
players = [("Ana", 90), ("Lokesh", 95), ("Bea", 90)]
ranked = sorted(players, key=lambda p: (-p[1], p[0]))
# [('Lokesh', 95), ('Ana', 90), ('Bea', 90)]
The same idea finds the best or worst element with max() and min(), as in max(players, key=lambda p: (p[1], p[0])).
5. Comparing Version Numbers
Version strings compare wrongly as text, because “3.9” > “3.10” is True character by character. Converting them to tuples of integers gives the right order, and sys.version_info is already such a tuple.
import sys
def version(text):
return tuple(int(part) for part in text.split("."))
text_order = "3.9" > "3.10" # True, wrong
tuple_order = version("3.9") > version("3.10") # False, right
new_enough = sys.version_info >= (3, 11) # True on Python 3.14
6. Tuple Comparison FAQs
Lists, named tuples and old Python 2 code each give a different answer.
6.1. Can we compare a tuple with a list?
Equality returns False even for the same elements, (1, 2) == [1, 2], and ordering raises TypeError. We convert one side first, for example tuple(my_list).
6.2. How do we compare named tuples?
A namedtuple is a tuple, so it compares by position in the same way, and the field names play no role. A dataclass declared with @dataclass(order=True) gets < and > that compare its fields in declaration order, the same way.
6.3. What happened to cmp() from Python 2?
It was removed in Python 3. The expression (a > b) – (a < b) gives the old -1, 0 or 1 result for tuples and other comparable values.
7. Conclusion
Tuples compare element by element from the left, and the first difference decides the result. That makes tuple keys the simplest way to sort by several fields and to compare versions, while all() with zip() answers the different question of whether every element is smaller.
8. References
Happy Learning !!
t1 = (1,2,3)
t2 = (3,2,1)
t1 and t2 are two tuples having 3 element each ,,,,so how can we compare it such that they are equal as they both have same elements in different order ,,,that is 1,2,3
You can compare it using reduce method for accumulating values of each tuple and then do your comparison .