Tuples: Creating, Unpacking, and Using Immutable Sequences
The items() method converts a dictionary into a list of tuples, where each tuple contains one key-value pair.
From Entries to Tuples
A dictionary stores information as key-value pairs. The items() method makes those relationships available as tuple-based entries. When you use list(d.items()), the result is a list in which every element is a tuple containing one key and its corresponding value.
The transformation preserves each key-value relationship. It changes the representation: dictionary entries become two-item tuples inside a list.
Tracing the Conversion
What do you think happens?
Suppose keys are added to a dictionary in the order mango, apple, zebra. When the dictionary is converted with list(d.items()), will the tuples be alphabetized or retain the insertion order?
Reveal answer
Answer: They will retain the order in which the keys were added.
The tuples returned from the dictionary appear in insertion order, not alphabetical order. In this example, the tuple for mango appears before apple, even though apple comes first alphabetically.
[("mango", 4), ("apple", 2), ("zebra", 7)]The call to items() exposes the dictionary's key-value pairs, and list() creates an actual list that can be stored, printed, or passed to a function. Each list element is a tuple. The order follows the order in which the keys were added: mango first, apple second, and zebra third.
Inside Each Pair
Every tuple produced from a dictionary item contains exactly two elements. The key is at index 0, and the value is at index 1. This fixed structure lets you retrieve the two parts with ordinary tuple indexing.
In this example, pair[0] refers to the key and pair[1] refers to the value. The positions are not interchangeable: using index 1 when you need the key would retrieve the value instead.
Insertion Order Matters
The tuples appear in insertion order, meaning the order in which the dictionary's keys were added. This is different from alphabetical order. A key that comes later alphabetically can still appear earlier in the list if it was added earlier to the dictionary.
Unpacking During Iteration
Because every item is a two-element tuple, its key and value can be unpacked into two variables during iteration. The first variable receives the key at index 0, and the second variable receives the value at index 1.
mango 4
apple 2On the first iteration, the tuple for mango is unpacked so that key refers to mango and value refers to 4. On the next iteration, the variables receive the key and value from the next tuple. This is useful when processing both parts of every dictionary entry together.
Immutable Pair Records
Each item tuple is immutable. Once the tuple has been created, its two elements cannot be changed. This fixed structure makes a tuple suitable for representing a key-value relationship that should remain together as one pair.
The important idea is that immutability applies to the tuple's contents after creation. The tuple can be stored in a list, passed to a function, or unpacked for iteration, but its key and value positions are fixed.
Mistakes to Avoid
Expecting items() to sort keys alphabetically
The tuples follow dictionary insertion order, not alphabetical order.
Fix:
Treat the order as the order in which the keys were added. Sort the resulting data separately if alphabetical order is needed.Treating each returned item as a single value
Each item is a tuple with exactly two elements: the key at index 0 and the value at index 1.
Fix:
Use two variables when unpacking, or use index 0 for the key and index 1 for the value.Assuming items() itself has already produced a stored list
The source distinguishes the dict_items result from the actual list created by wrapping the call with list().
Fix:
Use list(d.items()) when you need a list of the key-value tuples.
Practical Uses
Use items() when a task needs keys and values together. Iteration is one common use: each tuple can be unpacked into a key variable and a value variable. Converting with list(d.items()) is useful when the tuple-based data must be stored, printed, or passed to a function that expects a sequence of tuples.
| Need | Useful form | Reason |
|---|---|---|
| Process keys and values together | d.items() in iteration | Each item supplies a key-value tuple that can be unpacked |
| Store dictionary entries as a list | list(d.items()) | The result is an actual list of key-value tuples |
| Access one pair's parts | pair[0] and pair[1] | Index 0 is the key and index 1 is the value |
Choosing a tuple-based form for common dictionary tasks
Practice Check
A dictionary receives the keys zebra, apple, and mango in that order. Describe the order of the tuples in list(d.items()). Then explain which position in each tuple contains the key and which position contains the value.
Hints
- Use insertion order, not alphabetical order.
- Each item tuple has exactly two positions.
- The key is at index 0 and the value is at index 1.
Reading a Tuple-Based Dictionary Entry
A tuple from a dictionary's items is ("zebra", 7). Identify its key and value.
Read index 0: The element at index 0 is the key, so the key is zebra.
Read index 1: The element at index 1 is the value, so the value is 7.
The key is zebra and the value is 7.
Key Takeaways
- The items() method exposes a dictionary's key-value pairs as two-element tuples.
- Use list(d.items()) when an actual list of those tuples is needed for storage, printing, or passing to a function.
- In every item tuple, index 0 contains the key and index 1 contains the value.
- The tuples appear in dictionary insertion order, not alphabetical order.
- Because the tuples are immutable, their key and value positions cannot be changed after creation.