Comparing and Sorting Data Structures
Tuples are immutable sequences indexed by integers, meaning their elements cannot be changed after creation.
A Sequence That Locks Its Contents
Tuples look similar to lists because both are sequences whose elements are accessed by integer positions. The important difference is what happens after creation. A list is flexible: its items can be added, removed, or modified. A tuple is fixed: once it exists, its elements cannot be changed. This makes a tuple useful when a group of related values should stay together and should not be accidentally modified.
A tuple is an immutable sequence indexed by integers. Immutable means that the tuple's elements cannot be changed after the tuple has been created.
Think of a tuple as an ordered record whose positions are permanent. The order matters, and each value remains associated with its position. This fixed structure is useful for related data such as coordinates or RGB color values, where changing one part accidentally could create a bug.
Mapping Positions to Values
Tuple access follows the same integer-index idea used with lists. The bracket operator selects an element by position. Index 0 refers to the first element. Positive indices continue from the beginning, while index -1 refers to the last element. The index does not describe the value itself; it describes where that value appears in the ordered sequence.
Reading fixed-position data
Imagine a tuple containing the three values north, east, and south in that order. Which value is selected by index 0, and which value is selected by index -1?
Find index 0: Index 0 selects the first element in the tuple, so it selects north.
Find index -1: Index -1 selects the last element in the tuple, so it selects south.
Preserve the order: The values remain in their original order. Accessing an element does not rearrange the tuple.
Index 0 selects north, and index -1 selects south.
What Immutability Prevents
After a tuple is created, its elements cannot be modified. The same constraint applies to changing the sequence by adding or removing elements. In contrast, lists are designed for situations where items need to be added, removed, or modified frequently.
Choosing a tuple for fixed data
A program stores a group of related coordinate values that should not be changed accidentally. Should the program use a list or a tuple?
Identify the required behavior: The coordinate values are intended to stay fixed after creation.
Match the structure to the behavior: A tuple is an immutable sequence, so its elements cannot be changed after creation.
Contrast the alternative: A list would be more appropriate if the program needed to add, remove, or modify coordinate elements frequently.
Use a tuple when the coordinate sequence should remain fixed; use a list when the sequence needs flexible changes.
Comparing Tuples by Order
Tuples support equality and ordering comparisons. Python compares tuples element by element from left to right. For equality, two tuples must have the same length and all corresponding elements must be equal. For ordering, Python compares the first elements. If those elements are equal, it moves to the second elements, continuing from left to right until the comparison is decided.
Finding the first difference
Compare the tuple with elements 2 and 9 to the tuple with elements 3 and 1.
Compare the first elements: The first tuple begins with 2, and the second tuple begins with 3.
Stop at the deciding position: Because the first elements are different, the comparison is decided at the first position. The later values do not need to decide the ordering.
Apply the same rule to equality: For tuples to be equal, every corresponding element must be equal and the tuples must have the same length.
Tuple comparison proceeds from the first element toward the last, and the first unequal position determines an ordering comparison.
The same sequence supports other common operations as well. Tuples can be concatenated, repeated, checked for membership, and sliced. These operations work with the tuple sequence, while the immutability rule still prevents changing the existing tuple's elements.
Tuples as Dictionary Keys
A tuple's immutability makes it hashable. Because tuples are hashable, they can be used as dictionary keys. This allows a fixed sequence of values to identify a dictionary value. Lists do not have this capability among the sequence types discussed here.
Selecting a key structure
A program needs to associate one value with a fixed sequence of related values. Which sequence type is suitable as the dictionary key?
Check whether the sequence is fixed: The sequence is intended to remain unchanged after it is created.
Check the key requirement: A tuple is immutable and therefore hashable.
Choose the sequence: The tuple can serve as the dictionary key, whereas a list cannot serve this role among the sequence types being compared.
Use a tuple when a fixed sequence must act as a dictionary key.
Mistakes to Avoid
Treating a tuple like a list that can be edited in place.
Tuples are immutable, so their elements cannot be changed after creation.
Fix:
Choose a list when the sequence must change frequently. Choose a tuple when the contents should remain fixed.Forgetting that tuple access is position-based.
Tuple elements are accessed using integer positions and the bracket operator.
Fix:
Count from index 0 for the first element, and use index -1 for the last element.Comparing only the final elements of two tuples.
Tuple ordering comparisons proceed from left to right, and the first unequal position decides the ordering.
Fix:
Compare corresponding elements beginning with the first position.Using a list when a fixed sequence must be a dictionary key.
Tuples are hashable because they are immutable; lists do not have this capability among the sequence types discussed here.
Fix:
Use a tuple when the sequence should act as a dictionary key.
Practice Check
A fixed sequence contains the values 10, 20, and 30 in that order. Identify the value at index 0 and the value at index -1. Then decide whether a tuple or a list is the better structure if the values must not be changed. Finally, explain why the tuple could be used as a dictionary key.
Hints
- Index 0 refers to the first element.
- Index -1 refers to the last element.
- Use immutability to decide between a tuple and a list.
- Connect immutability to hashability.
What do you think happens?
Two tuples are compared. Their first elements are different, but their later elements are equal. Which position determines the ordering comparison?
Reveal answer
Answer: The first position
Tuple ordering comparisons proceed from left to right. The first unequal pair of corresponding elements determines the ordering.
Key Takeaways
- Tuples are ordered sequences indexed by integers.
- Index 0 selects the first element, and index -1 selects the last element.
- Tuples are immutable, so their elements cannot be changed after creation.
- Tuple comparisons proceed element by element from left to right.
- Immutability makes tuples hashable and suitable as dictionary keys, while lists are better when sequence contents must change.
Key Takeaways
- A tuple is an immutable, ordered sequence whose elements are accessed by integer positions.
- Use index 0 for the first element and index -1 for the last element.
- Unlike lists, tuples cannot have their elements added, removed, or modified after creation.
- Python compares tuples from left to right, using the first unequal position to determine ordering.
- Tuples are hashable and can be used as dictionary keys when a fixed sequence must identify a value.