Iterating Through Dictionaries with For Loops
A for loop can traverse a dictionary whose keys are tuples.
One Key, Two Values
A dictionary key does not have to contain only one value. A tuple can combine two related values into one composite key. The dictionary then connects that complete tuple key to one result. This is useful when the pair of values identifies the entry more precisely than either value by itself.
Retrieving the Matching Value
To retrieve a value, construct the same tuple key in the same order. The expression dictionary[value1, value2] retrieves the value associated with that specific tuple key. The key must represent the particular pair you want to find.
roles = {}
roles["Lovelace", "Ada"] = "Analyst"
role = roles["Lovelace", "Ada"]
print(role)- 1The assignment creates the tuple key (Lovelace, Ada).
- 2The lookup constructs that same tuple key and uses it to access the dictionary value.
- 3The result of the lookup is Analyst.
- The empty dictionary is prepared for entries.
- A tuple key containing two related values is assigned the value Analyst.
- The same tuple format retrieves the associated value.
- The retrieved value is displayed.
AnalystOrder Matters in Tuple Keys
Tuple keys are immutable and order-sensitive. That means (last, first) and (first, last) are different keys because their elements occur in different positions. Both can exist as separate keys in the same dictionary. A lookup must use the tuple arrangement that identifies the intended entry.
| Tuple key | Meaning for lookup |
|---|---|
| ("Lovelace", "Ada") | One specific key arrangement |
| ("Ada", "Lovelace") | A different key arrangement |
Changing the order changes the tuple key.
Unpacking Keys During Iteration
A for loop can traverse a dictionary whose keys are tuples. With tuple assignment in the loop, the two elements of each tuple key are unpacked automatically into separate variables. This lets the loop work with each related value independently while still traversing the dictionary by its complete composite key.
Lovelace Ada
Hopper GraceChoosing the Key Shape
Use a composite key when multiple values belong together and the pair is needed to distinguish one dictionary entry from another. A single-value key represents only one identifying value. A tuple key preserves both related values as one key, so the dictionary can distinguish entries using the complete pair.
Representing a Related Pair
Create a dictionary entry for the pair Lovelace and Ada, retrieve its value, and then unpack the pair while traversing the dictionary.
- 1Create the dictionary
Start with an empty dictionary so that a tuple key can be added.
- 2Create the composite key
Use roles["Lovelace", "Ada"] to create a tuple key containing both related values.
- 3Store the result
Assign Analyst to the tuple key so the complete pair maps to that result.
- 4Retrieve the result
Use roles["Lovelace", "Ada"] again. The same tuple format retrieves Analyst.
- 5Traverse the key
Use for last, first in roles so the tuple elements become the separate variables last and first.
The pair is preserved as one composite key, its value can be retrieved with the same ordered pair, and tuple assignment separates the pair during iteration.
Practice the Traversal
Suppose a dictionary uses the tuple key ("Hopper", "Grace"). Write the for-loop header that unpacks the first tuple element into last and the second tuple element into first.
Hints
- The loop needs two variables because each key contains two tuple elements.
- Place last first and first second so the variable positions match the tuple positions.
- The loop traverses the dictionary itself.
What do you think happens?
What values will the loop variables receive when the current dictionary key is ("Hopper", "Grace") and the loop header is for last, first in roles?
Reveal answer
last receives Hopper and first receives Grace
Tuple assignment unpacks the first tuple element into the first loop variable and the second tuple element into the second loop variable.
Mistakes with Composite Keys
Common mistake Looking up a tuple key in the wrong order.
What it looks likeAn entry was created with ("Lovelace", "Ada"), but the lookup uses ("Ada", "Lovelace").
Why it failsTuple keys are order-sensitive, so the two arrangements are different keys.
Do this insteadUse the same values in the same order when retrieving the value.
Common mistake Treating one tuple element as the entire key.
What it looks likeTrying to retrieve the entry with only "Ada".
Why it failsThe dictionary entry is associated with the complete tuple key, not with one element by itself.
Do this insteadConstruct the complete tuple key containing both related values.
Common mistake Reversing the loop variables.
What it looks likeUsing for first, last in roles when the tuple is arranged as (last, first).
Why it failsTuple assignment follows position, so the first variable receives the first tuple element.
Do this insteadArrange the loop variables in the same positional order as the tuple elements.
Key Takeaways
- A tuple can combine multiple related values into one composite dictionary key.
- Create and retrieve a tuple-key entry with the same ordered tuple format.
- Tuple keys are immutable and order-sensitive, so reversing their elements produces a different key.
- A for loop can use tuple assignment to unpack each tuple key into separate variables.
- Composite keys preserve multiple related values when a single-value key would not distinguish the complete pair.