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.

dictionarykey-value pairslisttuples: (key, value)list(d.items())
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.

Code walkthrough
d = {"pear": 3, "apple": 5, "orange": 2}
d_pairs = list(d.items())
d_pairs.sort()
print(d_pairs)
  1. 1
    The variable d refers to the original dictionary.
  2. 2
    The variable d_pairs refers to the newly created list of tuples.
  3. 3
    Each tuple places its key first, so the keys are the values used for the default ordering.
  4. 4
    After 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.
Output
[("apple", 5), ("orange", 2), ("pear", 3)]
("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: pearsame tuplesame tuplesame tuple
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.

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

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

  1. 1
    Locate the key

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

  2. 2
    Use the default comparison position

    The list of tuples is sorted by the first element of each tuple.

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

Result

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.

doriginal dictionarydoriginal dictionaryd_pairslist of tuplesd_pairssorted list of tuplesunchangedsort()
Which object changes after sorting, and does the original dictionary's data or structure change?
Before
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.

After
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 like

    Treating the order in which dictionary pairs appear as a reliable alphabetical order.

    Why it fails

    Dictionaries do not guarantee a predictable order.

    Do this instead

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

  • Common mistake

    Forgetting that each item is a tuple.

    What it looks like

    Thinking that the list contains separate keys in one part and values in another part.

    Why it fails

    Each list item contains one complete key-value pair.

    Do this instead

    Read 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 like

    Sorting d_pairs and then assuming d itself has been sorted.

    Why it fails

    The sorted list is separate from the original dictionary.

    Do this instead

    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.
Worked example

Checking the Practice Result

Sort the pairs from d = {"zebra": 1, "ant": 2, "monkey": 3} by key.

  1. 1
    Convert

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

  2. 2
    Read the tuple layout

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

  3. 3
    Sort

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

  4. 4
    Check the original

    The sorting operation applies to the separate list of tuples, not to the original dictionary.

Result

The sorted list is [("ant", 2), ("monkey", 3), ("zebra", 1)], while d remains the original dictionary object.

Key Takeaways

  1. 1
    A dictionary does not guarantee a predictable order for its key-value pairs.
  2. 2
    list(d.items()) converts the dictionary contents into a list of tuples.
  3. 3
    Each tuple stores the key at index 0 and the value at index 1.
  4. 4
    Calling .sort() on the list sorts by the first tuple element, placing keys in ascending alphabetical order by default.
  5. 5
    The 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.

Related concepts

More concepts starting with U