PythonArraysIntermediate

list.sort()

Sorts a list in place (stable), optionally using a key function.

Review the syntaxStudy the examplesOpen the coding app
my_list.sort(key=None, reverse=False)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Sorts a list in place (stable), optionally using a key function.

The list.sort() method performs an in-place sort of a list using the Timsort algorithm, which offers O(n log n) time complexity. Unlike the sorted() built-in, this method mutates the original list reference and returns None. The syntax list.sort(key=callbackFn, reverse=False) allows for powerful customization; the key parameter accepts a callable that transforms elements before comparison, while reverse=True enables descending order. Python's sort is stable, meaning elements with equal keys preserve their original relative positioning. For complex data like date strings, it is recommended to use ISO strings or a numeric timestamp for reliable ordering. Note that in Python 3, attempting to sort a list containing incompatible types (e.g., integers and strings) will result in a TypeError rather than falling back to an arbitrary order.

Quick reference

Syntax

my_list.sort(key=None, reverse=False)

Inputs

Parameters

key (optional)Callable[[Any], Any] · Function that returns a value to sort by.
reverse (optional)bool · Sort descending if True (default False).

See it in practice

Examples

1

Custom Key Sorting

words = ['banana', 'apple', 'cherry', 'kiwi']
words.sort(key=len)
print(words)
Output:
['kiwi', 'apple', 'banana', 'cherry']

Sorts the list based on the length of each string using the built-in len function as the key.

2

Sorting a List of Dictionaries

users = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25},
    {'name': 'Charlie', 'age': 35}
]
users.sort(key=lambda user: user['age'])
print(users)
Output:
[{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}]

Uses a lambda function to sort a list of dictionaries by a specific numeric value.

3

Descending Order with Stability

scores = [42, 10, 77, 10, 25]
scores.sort(reverse=True)
print(scores)
Output:
[77, 42, 25, 10, 10]

Sorts integers in descending order. Equal values (10) maintain their relative stability.

Debug faster

Common Errors

1

LogicError

Cause: Assuming sort() returns the sorted list and assigning it to a variable.

Fix: Call sort() on the list and then use the list variable itself, as sort() returns None.

my_list = [3, 1, 2]
my_list = my_list.sort()  # my_list is now None
2

TypeError

Cause: Attempting to sort a list containing unorderable types, such as mixing strings and integers.

Fix: Ensure all elements in the list are of compatible types or use a key function to convert them to a common type.

mixed_list = [10, '20', 5]
mixed_list.sort()  # Raises TypeError

Runtime support

Compatibility

Python 3.8+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Sorts a list in place (stable), optionally using a key function.

key: Function that returns a value to sort by. reverse: Sort descending if True (default False).

LogicError: Call sort() on the list and then use the list variable itself, as sort() returns None. TypeError: Ensure all elements in the list are of compatible types or use a key function to convert them to a common type.