Dictionary Fundamentals: Keys and Values
A tuple is a sequence of values whose elements cannot be changed after the tuple is created.
A Sequence That Stays Fixed
A tuple is a sequence of values whose elements cannot be changed after the tuple is created. It resembles a list because both sequences use integer positions for access, but a tuple is designed for data that should remain fixed. That fixed structure becomes especially useful when a sequence needs to serve as a dictionary key.
A fixed group of related values
Consider the tuple (51.5, -0.1), representing a pair of related values with a fixed structure.
- 1Create
The two values are grouped into one tuple sequence.
- 2Access
Position 0 refers to 51.5, while position 1 refers to -0.1.
- 3Protect
After creation, the tuple's elements cannot be replaced.
The tuple provides an ordered, fixed sequence that can be accessed by integer position.
Mapping Positions to Elements
Tuple positions are integer indices. Index 0 identifies the first element, and counting continues from there. Negative indexing counts from the end: index -1 identifies the last element. The bracket operator is used to access the element at a chosen index.
In the visual, the tuple is treated as the ordered sequence (north, east, west). The positive indices identify positions from the beginning, while -1 identifies the final element from the end. Indexing retrieves an element; it does not alter the tuple.
Why Assignment Stops Working
Immutability means that once a tuple exists, its elements cannot be replaced, added, or removed. A list allows its contents to be modified after creation, but a tuple locks its contents in place. Therefore, an attempted change to an existing tuple element does not produce a modified tuple.
From Tuple to Dictionary Key
Tuples are hashable because they are immutable. This allows a tuple to be used as a dictionary key, whereas lists do not have this capability. The tuple acts as the key, and the dictionary associates that key with a value. A fixed sequence can therefore identify one dictionary entry.
Using a fixed pair as an identifier
Imagine a dictionary in which the tuple (52, 8) identifies one entry.
- 1Form the key
The two integers are grouped into an immutable tuple.
- 2Use the key
The tuple can identify a dictionary entry because tuples are hashable.
- 3Associate a value
The dictionary connects that tuple key with its associated value.
The tuple provides a fixed, hashable sequence that can function as a dictionary key.
The key idea is the connection between immutability and hashability. Because the tuple cannot change after creation, it is suitable for use as a stable dictionary key. A list is mutable, so it does not provide the same key capability.
Choosing Tuples or Lists
| Question | Tuple | List |
|---|---|---|
| Can elements be changed after creation? | No; the elements are immutable. | Yes; list contents can be modified. |
| Can it be used as a dictionary key? | Yes, because tuples are hashable. | No, because lists do not have this capability. |
| When is it appropriate? | When the sequence should remain fixed. | When items need to be added, removed, or modified frequently. |
Choose according to whether the sequence should be fixed or flexible.
Tuples are useful for fixed structures such as coordinates, RGB color values, grouped related data, and values returned together from a function. Lists are more suitable when the sequence must change frequently. The choice is based on whether the contents should be fixed or flexible.
Tuple immutability does not prevent useful sequence operations. Tuples support concatenation, repetition, membership checks, slicing, and element-by-element comparison. These operations produce or inspect sequence information without changing the existing tuple.
Mistakes with Fixed Sequences
Common mistake Treating a tuple like a list that can be edited in place.
What it looks likeTrying to replace one existing tuple element after the tuple has been created.
Why it failsTuples are immutable, so their elements cannot be changed after creation.
Do this insteadCreate a new tuple when a different fixed sequence is needed, or choose a list when the contents must change.
Common mistake Forgetting that indexing begins at 0.
What it looks likeAssuming index 1 identifies the first element.
Why it failsThe first tuple element is at index 0.
Do this insteadUse 0 for the first element and -1 for the last element.
Common mistake Choosing a list when a fixed sequence must be a dictionary key.
What it looks likeTrying to use a mutable list as the sequence-based key for a dictionary entry.
Why it failsLists do not have the hashable capability that allows tuples to serve as dictionary keys.
Do this insteadUse a tuple when the sequence is fixed and needs to function as a dictionary key.
Common mistake Assuming that being immutable means a tuple cannot be inspected or combined.
What it looks likeAvoiding slicing, membership checks, concatenation, or comparisons because the tuple cannot be edited.
Why it failsMany sequence operations work on tuples; immutability means the existing tuple is not changed.
Do this insteadUse supported sequence operations to inspect or produce sequence results while keeping the original tuple fixed.
Trace the Tuple
Consider the tuple (blue, yellow, black). Identify the element selected by index 0, the element selected by index 1, and the element selected by index -1. Then decide whether replacing the element at index 1 would change the existing tuple. Finally, explain why this tuple could serve as a dictionary key.
Hints
- Start counting positive indices from 0.
- Use -1 for the final element.
- Connect immutability to hashability.
A complete reasoning path is: index 0 selects blue, index 1 selects yellow, and index -1 selects black. Replacing the element at index 1 cannot modify the existing tuple because tuple elements are immutable. The tuple can serve as a dictionary key because its immutability makes it hashable.
Essential Takeaways
- 1A tuple is an immutable sequence whose elements are accessed with integer indices.
- 2Index 0 selects the first element, and index -1 selects the last element.
- 3An existing tuple cannot have its elements replaced, added, or removed.
- 4Tuple immutability makes tuples hashable, so tuples can be used as dictionary keys while lists cannot.
- 5Choose tuples for fixed data and lists for data that needs frequent modification.
At a glance
- Tuples are fixed, immutable sequences indexed by integers.
- Use 0 for the first tuple element and -1 for the last.
- Immutability prevents changes to existing tuple elements.
- Because tuples are hashable, they can be used as dictionary keys.
- Use a tuple for fixed data and a list for flexible data.