Concepts / Immutability and Tuple Properties

Immutability and Tuple Properties

Python compares tuples element-by-element from left to right, stopping at the first index where elements differ.

  • Programming

The First Pair Matters

When Python compares two tuples with operators such as less than, greater than, equal to, or less than or equal to, it does not treat each tuple as one indivisible object. It examines corresponding elements from left to right. The first unequal pair determines the result, so values later in the tuples may never affect the comparison.

What do you think happens?

Which tuple is less than the other: (0, 1, 2000000) or (0, 3, 4)?

  • (0, 1, 2000000) is less
  • (0, 3, 4) is less
  • They are equal
Reveal answer

Answer: (0, 1, 2000000) is less

Python compares index 0 first: 0 equals 0, so it continues. At index 1, 1 is less than 3, so the result is decided there. The values 2000000 and 4 at index 2 are ignored.

Walking Through the Indices

Think of tuple comparison as a left-to-right scan. Python first examines the elements at index 0. If they differ, Python immediately determines the comparison from those two values. If they are equal, Python moves to index 1. It repeats this process until it finds a difference or reaches the end of the common portion.

compare index 00 = 0, continuecomparison stops at index 10left tuple: 00right tuple: 01left tuple: 13right tuple: 32000000not reached4not reached
How does Python move through corresponding tuple positions, and where does it stop when it finds the first unequal pair?

Finding the Deciding Index

Compare (4, 2, 9) with (4, 5, 1).

Index 0: The first elements are both 4, so Python continues.

Index 1: The next elements are 2 and 5. Because 2 is less than 5, the first tuple is less than the second.

Index 2: The values 9 and 1 are not considered because the result was already determined at index 1.

(4, 2, 9) is less than (4, 5, 1).

Stopping at the First Difference

The comparison is short-circuiting: once Python finds two unequal corresponding elements, it makes the decision immediately and ignores every later position. A later element cannot reverse a result that an earlier element has already determined.

equal, continuedifferent, stopnot evaluated for the resultIndex 00 and 0Index 11 and 3Less than1 is less than 3Index 22000000 and 4
Why can Python determine the comparison result from an early pair of elements even when later elements are much larger or different?

The source material illustrates this with (0, 1, 2000000) and (0, 3, 4). Index 0 is equal in both tuples. At index 1, 1 is less than 3, so the first tuple is less than the second. The value 2000000 never affects the result, even though it is larger than 4.

Equal Prefixes and Length

If all elements in the shared positions are equal and one tuple ends before the other, Python compares their lengths through the prefix rule: the shorter tuple is less than the longer tuple. In other words, an identical beginning does not make the tuples equal when one tuple has additional elements.

equalequalend of shorter tupleshorter is less2first tuple2second tupleShorter tupleless than longer tuple5first tuple5second tuple8additional element
What happens after Python compares an identical prefix and reaches the end of the shorter tuple?

The Prefix Rule

Compare (2, 5) with (2, 5, 8).

Compare index 0: Both tuples contain 2 at index 0, so Python continues.

Compare index 1: Both tuples contain 5 at index 1, so the shared elements remain equal.

Reach the end: The first tuple ends while the second tuple has one additional element.

(2, 5) is less than (2, 5, 8).

Reading Comparison Outcomes

Comparison patternPython's decision
The first differing pair has a smaller value in the first tupleThe first tuple is less than the second
The first differing pair has a larger value in the first tupleThe first tuple is greater than the second
All shared elements are equal and the first tuple ends firstThe first tuple is less than the second
All corresponding elements are equal and both tuples end togetherThe tuples are equal

Use the first unequal pair, or the prefix rule when no unequal pair exists.

Three Comparison Decisions

Predict the relationship in each pair: (3, 0) and (3, 1); (4, 7) and (4, 2); (5, 6) and (5, 6).

First pair: The values at index 0 are equal. At index 1, 0 is less than 1, so (3, 0) is less than (3, 1).

Second pair: The values at index 0 are equal. At index 1, 7 is greater than 2, so (4, 7) is greater than (4, 2).

Third pair: The corresponding elements are equal, so the tuples are equal.

(3, 0) is less than (3, 1); (4, 7) is greater than (4, 2); and (5, 6) equals (5, 6).

Sorting Tuple Collections

Tuple comparison is useful beyond isolated comparisons. Python's sorting functions rely on comparison operators to order items. When a list of tuples is sorted, Python applies the same left-to-right rule: an earlier element has priority, and a later element is considered only when earlier elements are equal.

For the list [(3, 1), (3, 0), (2, 5)], the tuple beginning with 2 comes first because 2 is less than 3. Between the two tuples beginning with 3, Python compares their second elements, placing (3, 0) before (3, 1).

Common Prediction Errors

  • Comparing the largest values anywhere in the tuples

    Python stops at index 1, where 1 is less than 3. The values at index 2 do not affect the result.

    Fix: Compare from index 0 toward the right and stop at the first unequal pair.

  • Continuing to use later elements after finding a difference

    The first unequal pair has already determined the result.

    Fix: Treat the first difference as the stopping point.

  • Assuming equal prefixes make tuples equal

    When the shared elements are equal, the shorter tuple is less than the longer tuple.

    Fix: After comparing the common elements, check whether one tuple ends first.

Practice the Scan

MEDIUM

For each pair, identify the first position that determines the result and state which tuple is less than, greater than, or equal to the other: (1, 8, 3) and (1, 9, 100); (6, 2) and (6, 2, 0); (7, 4, 900) and (7, 4, 900).

Hints
  • Begin with index 0 in every pair.
  • Stop as soon as the corresponding values differ.
  • If the shared values are equal, check which tuple ends first.

Key Takeaways

  1. Python compares tuples element by element from left to right.
  2. The first unequal pair determines the comparison result immediately.
  3. Values in later positions are ignored once an earlier difference is found.
  4. If one tuple is a prefix of another, the shorter tuple is less than the longer tuple.
  5. Sorting lists of tuples uses the same comparison process.

Key Takeaways

  • Tuple comparison follows a left-to-right sequence of corresponding elements.
  • The first unequal index is the exact point where Python decides the result.
  • Later values, even very large ones, cannot change a result already determined earlier.
  • Equal shared elements lead to the prefix rule: the shorter tuple is less than the longer tuple.
  • The same rules explain how Python orders lists of tuples.