Dictionary Methods and Operations
Looping over a dictionary with for key in dict: iterates through the keys; use dict[key] to access the value.
A Key-by-Key Visit
A dictionary stores entries as key-value pairs. When a for loop iterates directly over a dictionary, each loop variable receives a key. To retrieve the value belonging to that key, use dictionary[key]. This creates a two-step process: the loop visits a key, and the dictionary lookup retrieves the matching value.
scores = { "Ava": 8, "Ben": 6, "Cara": 9 } for name in scores: print(name, scores[name])
Ava 8
Ben 6
Cara 9Filtering by Value
A loop can inspect every dictionary entry and use a conditional statement to decide which entries should be printed or processed. The key is still the item received by the loop. The value is obtained with dictionary[key], and the condition tests that value. Entries that satisfy the condition continue to the output; entries that do not satisfy it are skipped.
Selecting Scores Above a Threshold
Print only the names whose scores are greater than 7.
Visit a key: The loop receives one name from the scores dictionary.
Retrieve the value: scores[name] gives the score associated with that name.
Test the value: The condition checks whether the score is greater than 7.
Choose the output: Only entries that satisfy the condition are printed.
The loop filters dictionary entries by value rather than printing every entry.
Ava 8
Cara 9Direct and Sorted Iteration
There are two different looping choices. In direct iteration, for key in dictionary visits the dictionary's keys. In sorted iteration, list(dictionary.keys()) creates a separate list of keys, that list is sorted alphabetically, and the loop visits the sorted list. In both cases, dictionary[key] retrieves the value. The difference is the sequence in which keys are visited.
scores = { "Zoe": 7, "Ava": 8, "Mia": 9 } keys = list(scores.keys()) keys.sort() for name in keys: print(name, scores[name])
Ava 8
Mia 9
Zoe 7Mistakes to Avoid
Expecting the loop variable to contain a value
Direct dictionary iteration provides a key to the loop variable, not the corresponding value.
Fix:
Use scores[name] when the value is needed.Testing the key when the requirement is about the value
The condition is applied to the key instead of the value associated with that key.
Fix:
Test scores[name], such as if scores[name] > 7.Sorting the dictionary when only alphabetical output is required
The source describes sorting a separate list of keys; the original dictionary remains unchanged.
Fix:
Use keys = list(dictionary.keys()), sort keys, and loop through keys.Sorting keys but looking up values from the wrong object
The sorted list contains keys, while the values remain in the dictionary.
Fix:
Use dictionary[name] for the value lookup.
Practice the Three Patterns
Given a dictionary named temperatures, write a loop that prints every key and its value. Then modify the loop so it prints only entries whose value is greater than 20. Finally, create a separate sorted list of the keys and print every key-value pair in alphabetical key order.
Hints
- Use for key in temperatures for direct iteration.
- Use temperatures[key] to retrieve the value.
- Place the value comparison inside an if statement.
- Use list(temperatures.keys()) and sort the resulting list before the final loop.
What do you think happens?
Before writing the final loop, what should be sorted: the original dictionary or a separate list made from its keys?
Reveal answer
Answer: A separate list of the dictionary's keys
The keys are extracted with list(dictionary.keys()), that separate list is sorted, and the sorted keys are used to look up values in the original dictionary.
Working Rule
- Direct dictionary iteration visits keys, so use dictionary[key] to retrieve each matching value.
- A conditional inside the loop can filter entries by testing dictionary[key].
- For alphabetical output, extract the keys into a separate list and sort that list.
- After sorting, use each sorted key to look up its value in the original dictionary.
- Sorting the separate key list leaves the original dictionary unchanged.
Key Takeaways
- A for loop over a dictionary directly receives keys.
- Use dictionary[key] to access the value paired with the current key.
- Use conditional logic inside the loop to filter entries by value.
- To print entries alphabetically, sort a separate list created from the dictionary's keys.
- The original dictionary remains unchanged when the separate key list is sorted.