Concepts / Sorting Lists with .sort() and sorted()

Sorting Lists with .sort() and sorted()

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 Dictionary to Sortable Data

A dictionary contains key-value pairs, but the sorting operation in this lesson is performed on a list. The bridge between these two structures is list(d.items()). It takes the dictionary's items and represents them as a list of tuples, so the pairs can be sorted as list elements.

expose itemslist(...)dictionarykey-value pairsd.items()dictionary itemslist of tuples(key, value) pairs
How does dictionary key-value data flow into a list of tuples?

Preparing Dictionary Contents

Suppose d contains the key-value pairs pear: 4, apple: 2, and orange: 3. What structure does list(d.items()) create?

Start with the dictionary: The data is held as key-value pairs.

Read the items: d.items() provides the dictionary items for conversion.

Create the list: list(d.items()) represents each key-value pair as a tuple inside a list.

The resulting list contains the tuples (pear, 4), (apple, 2), and (orange, 3), with each key at position 0 and each value at position 1.

Why Tuple Position Matters

Each tuple produced from a dictionary item has two positions. The key is at index 0, and the value is at index 1. When .sort() is called on the list of tuples, Python sorts by the first element by default. Because the first element is the key, the default result is an ascending alphabetical order of keys.

first elementsecond element(apple, 2)tupleappleindex 0: compared first2index 1: value
What does each tuple contain, and which tuple element does Python compare first?
Tuple positionContentsRole in the default sort
Index 0Dictionary keyCompared first
Index 1Dictionary valueNot used first when sorting by default

The tuple layout explains why dictionary contents sort by key.

Alphabetical Key Order

Consider a list containing the generated tuple pairs (pear, 4), (apple, 2), and (orange, 3). The keys are pear, apple, and orange. Since .sort() examines the first element of each tuple, the pairs move into the order apple, orange, pear. The values stay attached to their original keys because each key and value remains inside the same tuple.

nextnext(apple, 2)key: apple(orange, 3)key: orange(pear, 4)key: pear
What order do the tuple pairs take when Python compares their keys alphabetically?

Sorting the tuple pairs does not separate the keys from their values. The pair (apple, 2) remains one tuple after sorting; only its position in the list changes.

Choosing .sort() or sorted()

OperationWhat it works withResult
.sort()The list of tuplesSorts that list
sorted()The data being sortedProvides a separately sorted list
sortsprovides.sort()existing listsorted listthe list is sortedsorted()separate resultsorted listseparate list
Which operation changes the existing list, and which operation creates and returns a separately sorted list?

The Dictionary Stays Unchanged

list(d.items()) creates a list representation of the dictionary's items. Sorting that list changes the order of the tuple elements in the list, not the dictionary itself. The original dictionary remains separate from the sorted list.

unchangedsorteddictionarysource key-value pairsdictionarysame dictionarytuple listitems before sortingtuple listsorted by key
What remains unchanged in the dictionary after its items have been copied into and sorted as a separate list?

When checking whether a sort changed your data, identify which object you passed to the sorting operation. If it was the list created from d.items(), inspect that list separately from the original dictionary.

Mistakes to Avoid

  • Trying to sort the dictionary contents without first converting the items to a list of tuples.

    The described sorting process uses list(d.items()) to create the sortable list.

    Fix: Convert the items with list(d.items()), then sort the resulting list.

  • Assuming the value controls the default order.

    Each tuple stores the key at index 0, and .sort() compares the first tuple element by default.

    Fix: Look at the keys first when predicting the default order.

  • Expecting the original dictionary to be reordered by sorting the tuple list.

    The sorted list is separate from the original dictionary.

    Fix: Treat the sorted tuple list and the dictionary as separate objects.

MEDIUM

A dictionary has the generated items banana: 5, apple: 8, and cherry: 2. Describe the list of tuples produced by list(d.items()), then state the alphabetical order of the tuple keys after the list is sorted by default. Finally, explain whether the original dictionary is modified.

Hints
  • Each dictionary item becomes one tuple.
  • The key is at tuple index 0.
  • The original dictionary and the sorted tuple list are separate.

Key Takeaways

  1. Use list(d.items()) to represent dictionary contents as a list of key-value tuples.
  2. Each tuple places the key at index 0 and the value at index 1.
  3. The default .sort() order uses the first tuple element, so tuple pairs are ordered by key alphabetically.
  4. The list of tuples is separate from the original dictionary, so sorting the list does not modify the dictionary.
  5. .sort() sorts the existing list, while sorted() provides a separately sorted list.

Key Takeaways

  • Convert dictionary items with list(d.items()) before sorting them as a list.
  • A tuple stores its key first and its value second.
  • Default sorting of the tuple list compares keys in ascending alphabetical order.
  • Sorting the tuple list does not modify the original dictionary.
  • .sort() operates on an existing list, whereas sorted() provides a separate sorted list.