Understanding Dictionaries and Key-Value Pairs
Dictionaries do not guarantee a predictable order, so you convert them to a list of tuples using list(d.items()) to enable sorting.
From Unordered View to Sortable Data
A dictionary stores data as key-value pairs, but a dictionary does not guarantee a predictable order for those pairs. When you need to sort the contents by key, first convert the dictionary into a list of tuples. The expression list(d.items()) performs that conversion, creating a list in which every tuple contains one key-value pair.
What do you think happens?
Suppose d contains the pairs "pear": 3, "apple": 5, and "orange": 2. What kind of structure does list(d.items()) produce?
Reveal answer
A list of key-value tuples
Each item becomes a tuple containing its key and value. The key is at index 0 of the tuple, and the value is at index 1.
Converting Pairs into Tuples
The conversion has two levels. First, d.items() represents the dictionary's key-value pairs. Wrapping that result in list(...) produces a list. Each element of the new list is a two-part tuple: the key occupies index 0, and the value occupies index 1. This shape is important because the key is placed consistently at the beginning of every tuple.
d_pairs is a list of tuples. Its items have the form (key, value), such as ("pear", 3), ("apple", 5), and ("orange", 2).Sorting by the First Element
After conversion, call .sort() on the list of tuples. By default, the list is sorted using the first element of each tuple. Because the key is at index 0, the keys determine the order. For keys represented as words, this produces ascending alphabetical order.
d = {"pear": 3, "apple": 5, "orange": 2}
d_pairs = list(d.items())
d_pairs.sort()
print(d_pairs)- 1The variable d refers to the original dictionary.
- 2The variable
d_pairsrefers to the newly created list of tuples. - 3Each tuple places its key first, so the keys are the values used for the default ordering.
- 4After
d_pairs.sort(), the tuples appear in apple, orange, pear order.
- The dictionary is assigned to d.
list(d.items())creates a list of key-value tuples.sort()orders the tuples by their first element, the key.- The resulting list is printed in ascending alphabetical order by key.
[("apple", 5), ("orange", 2), ("pear", 3)]Why Tuple Position Matters
The tuple gives every dictionary item the same internal layout: key first, value second. The default sort looks at the first element, so it reaches the key immediately. The value remains attached to its key because both are held together in the same tuple. Sorting therefore reorders complete key-value pairs rather than separating keys from values.
Following One Pair Through the Sort
A list contains ("pear", 3), ("apple", 5), and ("orange", 2). What determines the position of ("orange", 2) after sort()?
- 1Locate the key
In ("orange", 2), the key is "orange" and it is at index 0.
- 2Use the default comparison position
The list of tuples is sorted by the first element of each tuple.
- 3Place the complete tuple
"orange" comes after "apple" and before "pear" in ascending alphabetical order, so the complete tuple ("orange", 2) takes the middle position.
The value 2 stays attached to the key "orange" while the tuple moves into its alphabetical position.
List Changes, Dictionary Remains
The list produced by list(d.items()) is a separate object from the original dictionary. Calling .sort() on that list sorts the list of tuples. It does not modify the dictionary. This distinction matters: after sorting, you have an ordered list representation of the dictionary's pairs, while the original dictionary is still the original dictionary.
d = {"pear": 3, "apple": 5, "orange": 2}
d_pairs = list(d.items())d_pairs contains the dictionary's key-value tuples in its current list order.
d = {"pear": 3, "apple": 5, "orange": 2}
d_pairs = list(d.items())
d_pairs.sort()d_pairs contains the same tuples in ascending alphabetical order by key; d is not modified.
Mistakes Beginners Make
Common mistake Assuming the dictionary itself has a predictable order.
What it looks likeTreating the order in which dictionary pairs appear as a reliable alphabetical order.
Why it failsDictionaries do not guarantee a predictable order.
Do this insteadConvert the pairs with list(d.items()) and sort the resulting list.
Common mistake Forgetting that each item is a tuple.
What it looks likeThinking that the list contains separate keys in one part and values in another part.
Why it failsEach list item contains one complete key-value pair.
Do this insteadRead each item as (key, value): the key is at index 0 and the value is at index 1.
Common mistake Expecting sort() to modify the original dictionary.
What it looks likeSorting d_pairs and then assuming d itself has been sorted.
Why it failsThe sorted list is separate from the original dictionary.
Do this insteadUse the sorted list when you need the sorted representation; remember that sorting it does not modify d.
Practice the Conversion
Given d = {"zebra": 1, "ant": 2, "monkey": 3}, describe the result of these two steps: first, d_pairs = list(d.items()); second, d_pairs.sort(). Identify the key and value in the tuple ("monkey", 3), and state whether sorting d_pairs modifies d.
Hints
- Each tuple has the form (key, value).
- The key is at index 0, so it controls the default tuple-list sort.
- The original dictionary and the new list are separate objects.
Checking the Practice Result
Sort the pairs from d = {"zebra": 1, "ant": 2, "monkey": 3} by key.
- 1Convert
Use
list(d.items())to create a list of tuples: ("zebra", 1), ("ant", 2), and ("monkey", 3), in whatever order the dictionary provides. - 2Read the tuple layout
In ("monkey", 3), "monkey" is the key at index 0 and 3 is the value at index 1.
- 3Sort
Calling .sort() orders the tuples by their first elements, producing ant, monkey, zebra order.
- 4Check the original
The sorting operation applies to the separate list of tuples, not to the original dictionary.
The sorted list is [("ant", 2), ("monkey", 3), ("zebra", 1)], while d remains the original dictionary object.
Key Takeaways
- 1A dictionary does not guarantee a predictable order for its key-value pairs.
- 2
list(d.items())converts the dictionary contents into a list of tuples. - 3Each tuple stores the key at index 0 and the value at index 1.
- 4Calling .sort() on the list sorts by the first tuple element, placing keys in ascending alphabetical order by default.
- 5The sorted list is separate from the original dictionary, so sorting the list does not modify the dictionary.
At a glance
- Use
list(d.items())to represent dictionary contents as a list of key-value tuples. - The key is at tuple index 0, and the value is at tuple index 1.
- The default .sort() operation orders the tuple list by its first element, the key.
- Sorting produces ascending alphabetical order for the example's word keys.
- Sorting the tuple list does not modify the original dictionary.