Concepts / Sorting and the sort() Method

Sorting and the sort() Method

Lambda functions used as sort keys provide a temporary comparison value for each element without modifying the original list.

  • Programming
Interactive lab

Try it: Sorting with key= and reverse=

How sorted() and list.sort() compute key(item) once per item, order the items by those keys with Python's comparison rules, and keep equal keys in their original order — including py4e's sort-a-dict-by-value pattern.

How it works

  1. Decorate: call the key function once on every item (no key means the item itself is compared).
  2. Compare keys with Python's rules: ints by value, strs by code point ('Z' < 'a'), tuples item by item.
  3. Order the items by key; reverse=True puts the largest key first.
  4. Stable: items with equal keys keep their original relative order, also with reverse=True.
  5. sorted() returns a new list; list.sort() rearranges the list in place and returns None; [:N] keeps the first N.

Default run (12 steps): 5 items in their original order. Python will call lambda t: t[1] once per item. … sorted() returned a new list: result = [('fig', 5), ('kiwi', 5), ('pear', 3), ('apple', 3), ('date', 1)].

Simplified: Up to 10 items of one kind (ints, short ASCII words, (word, count) tuples or a small dict) and a fixed menu of key functions. The sorting algorithm itself (Timsort) is not shown — only what it guarantees: the order by key and stability.

Educational simulation

Loading the simulation…

A Different Way to Sort

Sorting does not always mean comparing each element in its original form. A lambda function can transform each element into a temporary comparison value, and sort() can use those values to decide the order. The elements themselves remain in their original form while their positions are reordered. List comprehensions solve a related but different problem: they create a new list by transforming and optionally filtering values from an existing list.

What do you think happens?

A list contains two point dictionaries in this order: {'x': 2, 'y': 5}, then {'x': 4, 'y': 1}. What order will result when sort() uses lambda p: p['x'] as its key?

  • The points stay in their original order

  • The point with x equal to 4 comes first

  • The point with x equal to 2 comes first

Reveal answer
Answer:

The point with x equal to 2 comes first, so the points stay in their original order.

The lambda receives each point and returns its x-coordinate. The comparison values are 2 and 4, and 2 comes before 4 in ascending order.

Tracing a Lambda Sort Key

A lambda is a small, unnamed function written inline. When it is supplied to sort() through the key parameter, sort() gives the lambda each element one at a time. The lambda returns a value, and that returned value becomes the basis for comparison. The original element is still the item that gets moved in the list; the returned key is only the temporary value used to determine ordering.

Point A{'x': 4, 'y': 1}4p['x']Sorted pointsPoint B, Point APoint B{'x': 2, 'y': 5}2p['x']lambda p: p['x']lambda p: p['x']4 compared with 22 comes first
How does sort() apply the lambda to each point, use the returned x-coordinate for comparison, and then reorder the original point elements?
Code walkthrough
points = [{'x': 4, 'y': 1}, {'x': 2, 'y': 5}]
points.sort(key=lambda p: p['x'])
print(points)
  1. 1
    points starts with the point whose x-coordinate is 4, followed by the point whose x-coordinate is 2.
  2. 2
    The lambda receives each point and extracts p['x'].
  3. 3
    The resulting comparison values are 4 and 2.
  4. 4
    The point with key 2 is placed before the point with key 4.
  5. 5
    The dictionaries themselves remain unchanged in content.
  • The first point produces the temporary key 4.
  • The second point produces the temporary key 2.
  • The points are ordered by those keys, while each dictionary keeps both of its original coordinates.
Output
[{'x': 2, 'y': 5}, {'x': 4, 'y': 1}]

Building Lists with Comprehensions

A list comprehension creates a new list from a source list. Its general structure is [expression for item in list if condition]. The expression determines what value is placed in the new list. The for clause takes elements from the source list, and the optional if condition decides which elements are included. Unlike a lambda used as a sort key, a comprehension is specifically a list-building operation.

[2, 3, 4]source list2i[4, 6, 8]i * 23i4itake next itemtake next itemtake next item2 * 2 = 43 * 2 = 64 * 2 = 8
How does each source number move through the expression and become an item in the new list?
python
Output (expected)
[4, 6, 8]
python
Output (expected)
[4, 8]

In the filtered example, the condition is checked for each source element. Only 2 and 4 pass the even-number condition, and the expression then transforms them into 4 and 8. The source list is not replaced; the comprehension collects its results in a new list.

Combining Transformation and Ordering

The two tools can be used in sequence. A list comprehension can first create a new collection of selected or transformed values. A lambda can then provide the comparison value needed to sort that collection. The comprehension builds the data; the lambda supplies sorting logic.

Point dictionariessource collectionPoint labelscomprehension outputx-coordinateslambda resultsOrdered labelssorted resultcomprehension transformslambda extracts keysort compares keys
What values are produced when a comprehension creates point labels and sort() then orders them by a lambda key?
Worked example

Sort Selected Points by Their X-Coordinate

Create a new list containing points whose x-coordinate is at least 2, then sort that new list by x-coordinate.

  1. 1
    Start with the source points

    The points are [{'x': 3, 'y': 1}, {'x': 1, 'y': 4}, {'x': 2, 'y': 2}].

  2. 2
    Filter with a comprehension

    The condition p['x'] >= 2 keeps the points with x-coordinates 3 and 2.

  3. 3
    Create a new list

    The comprehension produces [{'x': 3, 'y': 1}, {'x': 2, 'y': 2}].

  4. 4
    Sort with a lambda key

    lambda p: p['x'] returns 3 for the first remaining point and 2 for the second, so the point with x equal to 2 comes first.

Result

[{'x': 2, 'y': 2}, {'x': 3, 'y': 1}]

python
Output (expected)
[{'x': 2, 'y': 2}, {'x': 3, 'y': 1}]

Syntax Traps and Debugging

These features are compact, so a small syntax error can change or prevent the operation. Debug by separating the pieces: identify the lambda parameter and returned expression, then identify the comprehension expression, for clause, and optional condition. After that, trace one source element at a time to predict what should happen.

  • Common mistake

    Forgetting the colon in a lambda expression

    What it looks like
    lambda p p['x']
    Why it fails

    A lambda needs a parameter followed by a colon and then the expression it returns.

    Do this instead
    lambda p: p['x']
  • Common mistake

    Putting the comprehension clauses in the wrong order

    What it looks like
    [i for i * 2 in listone]
    Why it fails

    The comprehension begins with the output expression, followed by for item in source_list.

    Do this instead
    [i * 2 for i in listone]
  • Common mistake

    Treating a lambda as if it creates a list

    What it looks like
    key_function = lambda i: i * 2
    Why it fails

    A lambda takes an input and returns an output. It does not create a new list by itself.

    Do this instead

    Use a list comprehension when the goal is to collect transformed values into a new list.

  • Common mistake

    Confusing the sort key with the original element

    What it looks like

    Using p['x'] as though it replaces the whole point

    Why it fails

    The x-coordinate is only the temporary comparison value. The complete point is still the element that is reordered.

    Do this instead

    Read lambda p: p['x'] as extract the x-coordinate for comparison, not replace each point with its x-coordinate.

Before
values = [2, 3, 4]
result = [i for i * 2 in values]

The iteration clause is malformed.

After
values = [2, 3, 4]
result = [i * 2 for i in values]

The expression i * 2 is evaluated for each i from values, producing [4, 6, 8].

Practice and Review

MEDIUM

Given points = [{'x': 5, 'y': 0}, {'x': 2, 'y': 3}, {'x': 4, 'y': 1}], write code that creates a new list containing only points whose x-coordinate is greater than 2, then sorts that new list by the y-coordinate.

Hints
  • Use a list comprehension to filter the points.
  • The lambda parameter should represent one point.
  • The sort key should extract the y-coordinate.

What do you think happens?

What is the result of [i * 2 for i in [2, 3, 4] if i > 2]?

Reveal answer
Answer:

[6, 8]

The condition removes 2. The expression then doubles 3 and 4, producing 6 and 8.

  1. 1
    A lambda used as sort()'s key receives each element and returns a temporary comparison value. Sorting uses that value to choose the order, but the original elements remain the items being reordered. A list comprehension follows the pattern [expression for item in list if condition] and creates a new list. The expression transforms included items, while the optional condition filters them. When debugging, check lambda colons, comprehension clause order, and the result produced for each source element.

Key Takeaways

  • A lambda sort key transforms each element into a temporary value used for comparison.
  • The original list elements remain intact in form while their order is changed.
  • List comprehensions create new lists by transforming and optionally filtering source elements.
  • Tracing each element through the lambda or comprehension makes output easier to predict and syntax errors easier to find.

Related concepts

More concepts starting with S