Concepts / Understanding Dictionaries and Key-Value Pairs

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.

  • Programming

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?

  • A list of key-value tuples
  • A single tuple containing all keys
  • A sorted dictionary
  • A list containing only the values
Reveal answer

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.

list(d.items())dictionarykey-value pairslisttuples: (key, value)
How does the dictionary's key-value data change shape when it becomes a list of tuples?
python
Output
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)

same tuplesame tuplesame tuple("pear", 3)index 0: pear; index 1: 3("apple", 5)first element: apple("apple", 5)index 0: apple; index 1: 5("orange", 2)first element: orange("orange", 2)index 0: orange; index 1: 2("pear", 3)first element: pear
What happens to the tuple positions as the list is sorted by its first element, the key?

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.

first elements comparedfirst elements compared("apple", 5)key at index 0ascending orderapple before orange("orange", 2)key at index 0
How does Python compare tuples to determine which key-value pair comes first?

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()?

Locate the key: In ("orange", 2), the key is "orange" and it is at index 0.

Use the default comparison position: The list of tuples is sorted by the first element of each tuple.

Place 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.

unchangedsort()doriginal dictionarydoriginal dictionaryd_pairslist of tuplesd_pairssorted list of tuples
Which object changes after sorting, and does the original dictionary's data or structure change?

Mistakes Beginners Make

  • Assuming the dictionary itself has a predictable order.

    Dictionaries do not guarantee a predictable order.

    Fix: Convert the pairs with list(d.items()) and sort the resulting list.

  • Forgetting that each item is a tuple.

    Each list item contains one complete key-value pair.

    Fix: Read each item as (key, value): the key is at index 0 and the value is at index 1.

  • Expecting sort() to modify the original dictionary.

    The sorted list is separate from the original dictionary.

    Fix: Use the sorted list when you need the sorted representation; remember that sorting it does not modify d.

Practice the Conversion

EASY

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.

Convert: Use list(d.items()) to create a list of tuples: ("zebra", 1), ("ant", 2), and ("monkey", 3), in whatever order the dictionary provides.

Read the tuple layout: In ("monkey", 3), "monkey" is the key at index 0 and 3 is the value at index 1.

Sort: Calling .sort() orders the tuples by their first elements, producing ant, monkey, zebra order.

Check 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

  1. A 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.
  3. Each tuple stores the key at index 0 and the value at index 1.
  4. Calling .sort() on the list sorts by the first tuple element, placing keys in ascending alphabetical order by default.
  5. The sorted list is separate from the original dictionary, so sorting the list does not modify the dictionary.

Key Takeaways

  • 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.