Dictionary Methods: keys(), values(), and items()
Looping through a dictionary directly iterates over its keys, not its values.
The First Iteration
A dictionary contains key-value associations, so it is natural to wonder what a for loop produces when the dictionary itself appears after in. The important behavior is that direct iteration through a dictionary visits its keys, not its values. The loop variable receives one key at a time.
Tracing the Current Key
During each iteration, the loop variable holds the current key. The value is not placed into the loop variable automatically. To obtain it, use bracket notation with the current key: dictionary[key]. This creates a two-step trace: identify the key currently held by the loop variable, then use that key to retrieve its associated value.
counts = { "chuck": 1, "annie": 42, "jan": 100 } for key in counts: print(key, counts[key])
Insertion Order
Dictionary keys maintain insertion order. Therefore, when keys are added in a particular order and the dictionary is later traversed, the loop visits those keys in that same order. This makes the loop's behavior predictable: to predict the output, first identify the order in which the keys were inserted, then trace the value lookup for each key.
What do you think happens?
A dictionary has keys inserted in this order: red, green, blue. What key will the loop variable receive first when the dictionary is traversed?
Reveal answer
Answer: red
Dictionary keys maintain insertion order, so the first inserted key is visited first.
Methods and Iteration Choices
The topic includes keys(), values(), and items(), but the central iteration rule established here is about using the dictionary directly: direct iteration produces keys. Once the loop variable contains a key, bracket notation retrieves the corresponding value. The standard pattern for accessing both parts is for key in dict followed by dict[key].
| Loop expression | What the source establishes | How to access the associated value |
|---|---|---|
| for key in dictionary | The loop visits dictionary keys | Use dictionary[key] |
| keys() | Named as a dictionary method in this topic | The provided source does not specify additional behavior |
| values() | Named as a dictionary method in this topic | The provided source does not specify additional behavior |
| items() | Named as a dictionary method in this topic | The provided source does not specify additional behavior |
Mistakes Beginners Make
Expecting direct dictionary iteration to produce values
Direct iteration through a dictionary visits keys, so the variable named value actually receives a key.
Fix:
Use a key-oriented variable name, such as key, and retrieve the value with counts[key].Printing the key without looking up its value
This prints only the current key. It does not retrieve the value associated with that key.
Fix:
Print key together with counts[key] when both parts are needed.Ignoring insertion order when predicting output
Dictionary keys maintain insertion order, so the visitation sequence follows the order in which the keys were added.
Fix:
Write down the insertion sequence before tracing the loop.
When debugging a dictionary loop, make a small trace table with one row per iteration. Record the current key first, then evaluate dictionary[key] to find the associated value. This makes the order and the lookup step visible instead of leaving them implicit.
Practice the Trace
Predicting a key loop
Suppose a dictionary has keys inserted in the order first, second, third. A loop uses the dictionary directly and prints its loop variable. What sequence of values should be predicted?
Identify the loop input: The dictionary itself is the sequence used by the for loop, so the loop visits dictionary keys.
Identify the order: Dictionary keys maintain insertion order, so the first, second, and third keys are visited in that order.
Write the output sequence: The loop variable receives first, then second, then third.
first, second, third
A dictionary contains the keys alpha, beta, and gamma in that insertion order. Describe the value of the loop variable on each iteration. Then explain how you would retrieve the value associated with the current key.
Hints
- Direct dictionary iteration visits keys.
- Use the insertion order to determine the sequence.
- The associated value is retrieved with dictionary[key].
Key Takeaways
- Using a dictionary directly as the sequence in a for loop visits its keys, not its values.
- The loop variable holds one key at a time.
- Use bracket notation, dictionary[key], to retrieve the value associated with the current key.
- Dictionary keys maintain insertion order, so the visitation sequence is predictable.
- The standard pattern for accessing both parts is for key in dict followed by dict[key].
Key Takeaways
- A direct for loop over a dictionary iterates through keys.
- The current key can be used with bracket notation to retrieve its associated value.
- Dictionary keys are visited in insertion order.
- To predict a loop's output, trace the inserted keys from first to last.
- The standard key-and-value pattern is for key in dict followed by dict[key].