Sorting Lists of Tuples
The comparison is decided by the first unequal pair, not by the largest value anywhere in either tuple.
Try it: Tuples: Comparison and Unpacking
How Python compares tuples lexicographically — the first unequal pair of items decides, a shorter prefix is smaller, int vs str ordering raises TypeError — and how tuple assignment unpacks and swaps values.
How it works
- Walk both tuples from index 0 while the items are equal.
- At the first difference, that pair alone decides (later items are never looked at); strs compare by code point.
- If one tuple runs out first, the shorter one is smaller; ordering an int against a str raises TypeError.
- Unpacking checks the count first: too many or too few values raise ValueError before any name is bound.
- a, b = b, a builds the right-hand tuple from the old values, then unpacks it into the names.
Default run (4 steps): Evaluate (1, 'pear', 7) < (1, 'plum', 2): compare item by item from index 0. … 'pear' < 'plum' is True (strings compare by code point: 'e' is 101, 'l' is 108), so (1, 'pear', 7) < (1, 'plum', 2) is True.
Simplified: Flat tuples of up to 6 ints or short ASCII strs (no nested tuples, floats or starred targets).
Loading the simulation…
The First Difference
When Python compares two tuples, it does not treat each tuple as one indivisible object. It examines corresponding elements from left to right: first index 0, then index 1, then index 2, and so on. As soon as it finds a pair of unequal elements, that pair determines the comparison result. Python stops there and ignores every later element.
The comparison is decided by the first unequal pair, not by the largest value anywhere in either tuple.
A Comparison Trace
Finding the stopping index
Compare the tuples (0, 1, 2000000) and (0, 3, 4).
- 1Index 0
The values are 0 and 0. They are equal, so Python continues to the next position.
- 2Index 1
The values are 1 and 3. They differ, and 1 is less than 3. This determines that the first tuple is less than the second.
- 3Index 2
Python does not use 2000000 and 4 to decide the result because the comparison already stopped at index 1.
(0, 1, 2000000) is less than (0, 3, 4).
Equal Prefixes
Tuple length matters only after every shared position has been found equal. If one tuple contains all the elements of the other in the same order and then ends, the shorter tuple is less than the longer tuple. The shorter tuple is called a prefix of the longer tuple in this situation.
Comparing different lengths
Compare the tuples (2, 5) and (2, 5, 0).
- 1Shared index 0
Both tuples contain 2, so the comparison continues.
- 2Shared index 1
Both tuples contain 5, so all shared elements are equal.
- 3End of the shorter tuple
The first tuple ends while the second tuple still has an element.
(2, 5) is less than (2, 5, 0).
Do not compare a missing element with the next element of the longer tuple. If all shared elements are equal, the tuple that ends first is less.
Later Values Ignored
A common mistake is to scan every value and decide based on a later value that looks more important or much larger. Python does not calculate an overall size for the tuple. It uses the first position where the tuples differ. Therefore, two tuples can contain very different later elements while still having a comparison result determined by an earlier position.
Common mistake Comparing the largest elements first
What it looks like(0, 1, 2000000) versus (0, 3, 4)Why it failsPython reaches the unequal values 1 and 3 at index 1 before it would consider index 2.
Do this insteadCompare from index 0 toward the right and stop at the first unequal pair.
Common mistake Continuing after finding an unequal pair
What it looks likeTreating the values 2000000 and 4 as part of the same decision
Why it failsThe comparison has already been decided by 1 and 3.
Do this insteadRecord the result at the first difference and ignore later positions.
Common mistake Assuming equal shared elements make different-length tuples equal
What it looks like(2, 5) versus (2, 5, 0)Why it failsOne tuple is a prefix of the other, so the shorter tuple is less than the longer tuple.
Do this insteadAfter matching all shared positions, check which tuple ends first.
Sorting Several Tuples
Python uses this same element-by-element comparison when sorting a list of tuples. It first orders tuples by their first elements. If two tuples have equal first elements, their second elements determine their relative order. The process continues to later positions only when earlier positions match.
Ordering a list of tuples
Determine the sorted order of [(3, 1), (3, 0), (2, 5)].
- 1Place the tuple beginning with 2
(2, 5) comes before both tuples beginning with 3 because 2 is less than 3.
- 2Compare the remaining tuples
(3, 1) and (3, 0) have equal first elements, so Python compares their second elements.
- 3Use the second elements
0 is less than 1, so (3, 0) comes before (3, 1).
[(2, 5), (3, 0), (3, 1)]
Prediction Practice
What do you think happens?
Which tuple is less: (4, 2, 900) or (4, 7, 1)?
Reveal answer
(4, 2, 900)
The values at index 0 are equal. At index 1, 2 is less than 7, so Python stops there and does not use 900 or 1.
Predict the sorted order of [(1, 9), (1, 2), (0, 8)]. State which index determines each important placement.
Hints
- Compare the first elements before considering the second elements.
- For the two tuples beginning with 1, compare their second elements.
Summary
- 1Python compares tuple elements from left to right.
- 2The first unequal pair determines the result, and later elements are ignored.
- 3If all shared elements are equal, the shorter tuple is less than the longer tuple.
- 4Large later values cannot overturn a result decided at an earlier index.
- 5Sorting a list of tuples uses the same comparison process.
At a glance
- Tuple comparison proceeds from the first position toward the last.
- The comparison stops immediately at the first unequal pair.
- A tuple that is an equal prefix of a longer tuple is less than that longer tuple.
- Later values, even very large ones, do not matter after an earlier difference.
- Python sorts lists of tuples by applying this same lexicographic comparison rule.