Iterating Over Dictionaries: keys(), values(), and items()
The items() method converts a dictionary into a list of tuples, where each tuple contains one key-value pair.
From Mappings to Tuples
A dictionary stores relationships between keys and values. The items() method is useful when you need to work with both parts of every relationship at the same time. In the tuple-based form, each key-value relationship becomes one tuple, and the collection of those tuples can be stored as a list.
The important transformation is dictionary data into a list whose elements are two-element tuples: one key followed by its corresponding value.
Tracing the Transformation
What do you think happens?
Suppose keys are added in the order b, a, c. In what order will the key-value tuples appear?
Reveal answer
Answer: Insertion order: b, a, c
The tuples are returned in the order the keys were added to the dictionary, not in alphabetical order.
The visual shows the central idea: the dictionary's key-value relationships are preserved, but their representation changes. After conversion to a list, every element is a tuple containing one complete relationship.
Inside Each Pair
An item tuple contains exactly two elements: the key at index 0 and the value at index 1.
The two positions have fixed roles. Index 0 identifies which dictionary key is being represented, while index 1 contains that key's corresponding value. The tuple is immutable, so once the tuple has been created, its two-element structure cannot be changed.
Reading One Item
Interpret the tuple (apple, 5) as one dictionary item.
First position: The element at index 0 is apple, so apple is the key.
Second position: The element at index 1 is 5, so 5 is the value associated with apple.
Relationship: Together, the tuple represents one complete key-value relationship.
The tuple contains exactly two elements: key apple at index 0 and value 5 at index 1.
Choosing the Right View
| Method | Information emphasized | Useful when |
|---|---|---|
| keys() | Keys alone | You need the identifying parts of the dictionary relationships |
| values() | Values alone | You need the stored results without the keys |
| items() | Each key together with its value | You need to process or store complete key-value relationships |
Order of Returned Items
Predicting the Pair Sequence
A dictionary is built by adding the keys b, a, and c in that order. What tuple order should you expect after converting its items to a list?
Record the insertion sequence: The keys were added in the order b, then a, then c.
Keep each relationship together: Each key remains paired with its corresponding value in a two-element tuple.
Apply the ordering rule: The tuple sequence follows insertion order rather than alphabetical order.
The pairs appear in b, a, c order. They are not rearranged into a, b, c order.
Storing and Iterating Pairs
When a dictionary's items are iterated, the useful unit is one complete tuple at a time. Because the tuple has a predictable two-element structure, its key and value can be handled together. Converting with list(d.items()) creates an actual list that can be stored, printed, or passed to a function.
Consider a dictionary whose keys were inserted in the order mango, apple, zebra. Describe the tuple sequence produced by list(d.items()), and identify the key and value positions within any one tuple.
Hints
- Use insertion order, not alphabetical order.
- The key is at index 0 and the value is at index 1.
- Each tuple contains exactly two elements.
Assuming items are returned alphabetically.
The tuples follow the order in which the keys were added.
Fix:
Track insertion order. Sort the resulting list separately only when alphabetical order is required.Treating an item tuple as if it contained only a key or only a value.
Every item tuple contains both parts of one key-value relationship.
Fix:
Read index 0 as the key and index 1 as the value.Assuming an item tuple can be changed after it is created.
The tuples returned by items() are immutable.
Fix:
Treat each tuple as a fixed representation of its key-value relationship.Forgetting to create a list when a stored list is required.
list(d.items()) is the operation identified for converting the dict_items object into an actual list.
Fix:
Use list(d.items()) when you need a list that can be stored, printed, or passed to a function.
Key Takeaways
- items() is the choice when both keys and values must remain together.
- The list form contains one two-element tuple for each key-value relationship.
- Within each tuple, the key is at index 0 and the value is at index 1.
- Tuple order follows dictionary insertion order, not alphabetical order.
- Use list(d.items()) when an actual list is needed for storage, printing, or passing to a function.
Key Takeaways
- items() keeps each key paired with its corresponding value.
- Each item is an immutable tuple containing exactly two elements.
- The key occupies index 0 and the value occupies index 1.
- Items appear in insertion order rather than alphabetical order.
- list(d.items()) produces an actual list of tuples for storage or further use.