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).
Compare position 1: The first elements are both 4, so there is no difference yet.
Compare position 2: The second elements are 2 and 7. This is the first difference, so the comparison is decided here.
Ignore 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.
Compare first positions: The tuple beginning with 1 comes before both tuples beginning with 2.
Resolve the matching first positions: The remaining two tuples both begin with 2, so compare their second elements, 8 and 3.
Place 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.
Decorate: Create tuples containing each derived score and its original value: (3, alpha), (1, beta), and (2, gamma).
Sort: 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).
Undecorate: 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
Looking at a later tuple element before checking earlier elements
The first elements differ, so the first position decides the order before the second position matters.
Fix:
Compare positions from left to right and stop at the first difference.Assuming every element in a tuple is always examined
The second elements, 2 and 7, are already different.
Fix:
Once a difference is found, ignore the remaining positions for that comparison.Expecting reverse=True to reverse only the primary criterion
reverse=True reverses comparison for all elements in the tuple, including tie-breaking comparisons.
Fix:
Use a custom key function when different criteria require finer control.Forgetting the undecorate step in DSU
Sorting produces tuples containing both derived keys and original values, but the desired final result may consist only of the original values.
Fix:
After 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
- Tuple comparison moves from left to right and stops at the first difference.
- The first tuple position is the primary sorting criterion; later positions resolve ties.
- A tuple can represent several sorting criteria in a chosen order.
- DSU decorates original values with sort keys, sorts the decorated tuples, and then extracts the original values.
- reverse=True reverses every comparison in the tuple, while a custom key function provides finer multi-criteria control.
Key Takeaways
- 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.