Comparing Sequences and Strings
Tuple comparison proceeds element by element from left to right, stopping as soon as a difference is found.
The First Difference Decides
When Python compares tuples, it checks their elements from left to right. It stops as soon as it finds a difference. Elements after that first difference do not affect the result.
Comparing Two Tuples
Determine which tuple comes first when comparing (4, 2, 9) and (4, 7, 1).
- 1Compare position 1
The first elements are both 4, so there is no difference yet.
- 2Compare position 2
The second elements are 2 and 7. This is the first difference, so the comparison is decided here.
- 3Ignore position 3
The third elements, 9 and 1, are not considered because an earlier difference has already determined the ordering.
(4, 2, 9) comes before (4, 7, 1).
Reading Tuple Order
To predict a sorting result, read each tuple from its first element onward. The first position has the first opportunity to decide the order. If two tuples match there, compare their second positions. Continue only while the earlier positions are equal.
Finding the Sorting Order
Arrange the tuple keys (2, 8), (1, 9), and (2, 3) in ascending order.
- 1Compare first positions
The tuple beginning with 1 comes before both tuples beginning with 2.
- 2Resolve the matching first positions
The remaining two tuples both begin with 2, so compare their second elements, 8 and 3.
- 3Place the tied-first tuples
The tuple with second element 3 comes before the tuple with second element 8.
(1, 9), (2, 3), (2, 8)
Building Multi-Criteria Keys
A tuple can hold several sort criteria in a deliberate order. Put the primary criterion first, the secondary criterion second, and later tie-breaking criteria after that. Sorting these tuples applies the same left-to-right comparison rule to the criteria.
Suppose records are represented by names and scores. A derived key such as (score, name) makes score the primary criterion. When two records have the same score, the name becomes the next criterion used to order them.
Tracing the DSU Pattern
The Decorate-Sort-Undecorate pattern, often called DSU, uses tuples to sort by derived values. First, create tuples containing sort keys and the original values. Next, sort the tuples. Finally, extract the original values from the sorted tuples.
Sorting by a Derived Score
Use DSU to order the original values alpha, beta, and gamma by the derived scores 3, 1, and 2.
- 1Decorate
Create tuples containing each derived score and its original value: (3, alpha), (1, beta), and (2, gamma).
- 2Sort
Compare the tuples by their first elements, which are the derived scores. The order of the decorated tuples becomes (1, beta), (2, gamma), (3, alpha).
- 3Undecorate
Extract the original values from the sorted tuples and remove the derived scores.
beta, gamma, alpha
Sequences and Strings
The central comparison idea is positional: examine elements from left to right and stop at the first difference. Tuple comparison provides the clearest model for this lesson, and the same way of thinking helps you inspect other sequence-like values by asking which position is compared first and where the first difference appears.
Common Sorting Mistakes
Common mistake Looking at a later tuple element before checking earlier elements
What it looks likeTreating (2, 3) as later than (1, 9) because 3 is smaller than 9
Why it failsThe first elements differ, so the first position decides the order before the second position matters.
Do this insteadCompare positions from left to right and stop at the first difference.
Common mistake Assuming every element in a tuple is always examined
What it looks likeUsing the final value in (4, 2, 9) to compare it with (4, 7, 1)
Why it failsThe second elements, 2 and 7, are already different.
Do this insteadOnce a difference is found, ignore the remaining positions for that comparison.
Common mistake Expecting reverse=True to reverse only the primary criterion
Why it failsreverse=True reverses comparison for all elements in the tuple, including tie-breaking comparisons.
Do this insteadUse a custom key function when different criteria require finer control.
Common mistake Forgetting the undecorate step in DSU
Why it failsSorting produces tuples containing both derived keys and original values, but the desired final result may consist only of the original values.
Do this insteadAfter sorting, extract the original values from the decorated tuples.
Practice the Comparison
Predict the ascending order of these tuple keys: (3, 4), (2, 9), (3, 1), and (2, 5). Explain which positions decide each comparison.
Hints
- Compare the first elements before looking at the second elements.
- For the two keys beginning with 2, compare their second elements.
- For the two keys beginning with 3, compare their second elements.
Trace a DSU process for the original values red, blue, and green when their derived keys are 2, 3, and 1. Write the decorated tuples, their sorted order, and the final undecorated result.
Hints
- Place each derived key before its original value.
- Sort the decorated tuples by their first elements.
- Remove the derived keys after sorting.
Key Takeaways
- 1Tuple comparison moves from left to right and stops at the first difference.
- 2The first tuple position is the primary sorting criterion; later positions resolve ties.
- 3A tuple can represent several sorting criteria in a chosen order.
- 4DSU decorates original values with sort keys, sorts the decorated tuples, and then extracts the original values.
- 5reverse=True reverses every comparison in the tuple, while a custom key function provides finer multi-criteria control.
At a glance
- Python compares tuples element by element from left to right.
- The first differing element decides the comparison, so later elements may never be considered.
- Tuple positions naturally express primary, secondary, and later sorting criteria.
- The DSU pattern sorts derived tuple keys and then returns the original values.
- reverse=True affects all tuple elements; use a custom key function for finer control.