Dictionary Basics: Keys, Values, and Methods
The .items() method returns tuples of (key, value) pairs from a dictionary.
Why Both Parts Matter
A dictionary stores relationships between keys and values. When a loop needs only the keys, a basic dictionary loop is enough. When the loop must use each key together with its associated value, use the .items() method. It returns key-value pairs as tuples, allowing the loop to receive both parts at once.
From Items to Variables
Calling d.items() produces key-value tuples. For a dictionary containing three entries, the result can be understood as three pairs, such as ('a', 10), ('b', 1), and ('c', 22). In a loop written as for key, val in d.items():, Python automatically unpacks one tuple during each iteration. The first part goes into key, and the second part goes into val. The variable names are chosen by the programmer; what matters is that the two loop variables receive the tuple's two parts.
d = {'a': 10, 'b': 1, 'c': 22} for key, val in d.items(): print(val, key)
Choosing an Iteration Method
Python dictionaries provide three main iteration methods. Use .keys() when the loop needs only keys, .values() when it needs only values, and .items() when it needs both keys and values. Choosing the method that matches the information required by the loop makes the code clearer and avoids trying to obtain information that the loop did not request.
| Method | Information supplied during iteration | Use it when |
|---|---|---|
| .keys() | Keys | Only the dictionary keys are needed |
| .values() | Values | Only the dictionary values are needed |
| .items() | Key-value tuples | Both the key and its value are needed |
The three main dictionary iteration choices
Tracing Every Loop
What do you think happens?
What will this loop print, and in what order?
Reveal answer
Answer: 10 a, then 1 b, then 22 c
Each item is unpacked as key and val. The print statement places val first and key second.
10 a
1 b
22 cThe most reliable way to predict this output is to separate the task into two questions for each tuple: which value is assigned to key, and which value is assigned to val? Then inspect the print statement to determine the display order. For ('a', 10), key receives 'a' and val receives 10, but print(val, key) displays 10 first. Repeating that process for the other two tuples gives the complete output.
Mistakes with Dictionary Loops
Expecting a basic dictionary loop to provide both parts of each entry
A basic dictionary loop gives the loop variable only the keys.
Fix:
Use for key, val in d.items(): when the loop needs both the key and its corresponding value.Using .keys() when the value is also needed
The iteration method supplies keys only, so the loop does not receive each key-value pair together.
Fix:
Use .items() and unpack each pair into two variables.Assuming the print order is determined by the tuple order
The tuple contains the key first and value second, but the print statement deliberately displays val before key.
Fix:
Trace the assignments first, then read the print arguments from left to right.Treating .values() as if it supplied keys and values
The .values() method is for iterating through values only, not key-value tuples.
Fix:
Choose .items() when both parts are required.
Choose the iteration method based on the loop's actual need: .keys() for keys, .values() for values, and .items() for both. When using .items(), name the two loop variables so their roles are clear, such as key and val. For output prediction, trace one tuple at a time instead of trying to read the entire loop at once.
Practice the Trace
Given d = {'x': 5, 'y': 8}, identify the values of key and val on each iteration of this loop, then write the two output lines: for key, val in d.items(): print(key, val)
Hints
- Treat each dictionary entry as one key-value tuple.
- The first loop variable receives the key and the second loop variable receives the value.
- Read print(key, val) from left to right.
Tracing an items() loop
Predict the output of for key, val in d.items(): print(key, val) when d is {'x': 5, 'y': 8}.
First tuple: The first pair is ('x', 5), so key receives 'x' and val receives 5. The line prints x 5.
Second tuple: The second pair is ('y', 8), so key receives 'y' and val receives 8. The line prints y 8.
The output is x 5 followed by y 8.
Key Takeaways
- The .items() method returns key-value pairs as tuples.
- In for key, val in d.items():, each tuple is automatically unpacked into key and val.
- Use .keys() for keys only, .values() for values only, and .items() for both.
- To predict output, trace each tuple, record the variables it assigns, and then follow the print statement's argument order.
Key Takeaways
- Use .items() when a loop needs both each dictionary key and its associated value.
- Each item is a tuple, and tuple unpacking places its first part in the first loop variable and its second part in the second loop variable.
- Use .keys(), .values(), or .items() according to the information the loop needs.
- Predict dictionary-loop output by tracing one key-value tuple at a time and checking the order used by print.