list.sort()

Reorder the list in place using Python's stable Timsort. Returns None on purpose — the API pushes you toward `sorted()` when you want a new list.

List methodPython 1.0+Live demo
Common call
items.sort()
Returns
None — the list itself is reordered
Replaces
building a sorted copy when the original is disposable
Watch out
the classic bug: `xs = xs.sort()` sets xs to None
list.sort(*, keykeyFunction of one argument used to extract a comparison key from each item. Keyword-only.type: callable · default: None=None, reversereverseIf True, sort descending. Stability is preserved either way. Keyword-only.type: bool · default: False=False)
None

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated items
reverseint1 = descending, empty = ascending
Output
['banana', 'apple', 'cherry'].sort()
None

The demo shows the LIST STATE after sorting. Python actually returns None; the meaningful effect is mutation. Sort is stable — equal items keep their original relative order. The demo input arrives as strings, so numeric cases sort lexicographically ("10" < "2") unless you convert first — a classic footgun documented in the pitfalls.

Parameters

NameTypeRequiredDescription
keycallableno (None)Function of one argument used to extract a comparison key from each item. Keyword-only.
reverseboolno (False)If True, sort descending. Stability is preserved either way. Keyword-only.

Return value

NoneReturns None — the useful effect is mutation. The demo shows the list state after sorting.

Common patterns

Sort by an attribute
The key= parameter lets you sort by any extraction — attribute, index, method result.
people.sort(key=lambda p: p.age)
Multi-key sort
Tuples compare lexicographically — first key wins, ties broken by the next.
rows.sort(key=lambda r: (r.dept, -r.salary))
Case-insensitive sort
str.lower as the key ignores case without mutating the strings themselves.
names.sort(key=str.lower)

Examples

1. Basic
xs = [3, 1, 2] xs.sort() xs
Returns
[1, 2, 3]
2. Reverse
xs = [3, 1, 2] xs.sort(reverse=True) xs
Returns
[3, 2, 1]
3. With key
xs = ["banana", "kiwi"] xs.sort(key=len) xs
Returns
["kiwi", "banana"]
4. Returns None (surprise)
["c","a","b"].sort()
Returns
None
5. Stable — ties keep order
xs = [("a", 2), ("b", 1), ("a", 1)] xs.sort(key=lambda t: t[0]) xs
Returns
[("a", 2), ("a", 1), ("b", 1)]

Pitfalls

1. The `xs = xs.sort()` bug
sort() returns None. Assigning its result back sets your variable to None — and the original list is now unreachable through xs. Probably Python's most-copied Stack Overflow mistake.
Now xs is None
xs = [3, 1, 2]
xs = xs.sort()
print(xs)
None
Two options
xs.sort()          # mutate, keep name
# or
xs = sorted(xs)     # new list, replace name
[1, 2, 3]
2. Can only sort a list — not iterables in general
sort is a list METHOD. Tuples, sets, dicts, generators, ranges do not have it. Reach for sorted() when you need to order any iterable.
AttributeError
(3, 1, 2).sort()
AttributeError: 'tuple' object has no attribute 'sort'
sorted() works
sorted((3, 1, 2))
[1, 2, 3]
3. Strings sort lexicographically
Sorting numeric strings gives lexical order — "10" comes before "2". Convert to int first if you meant numeric.
Lexical order
xs = ["10", "2", "1"]
xs.sort()
xs
["1", "10", "2"]
Numeric key
xs = ["10", "2", "1"]
xs.sort(key=int)
xs
["1", "2", "10"]
4. Mixed types raise TypeError (Python 3)
Comparing incompatible types is disallowed in Python 3 — sorting a list of mixed types blows up mid-scan.
Runtime error
xs = [1, "a", 2]
xs.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
Use a key
xs.sort(key=str)  # coerce for comparison
compares as strings

When to use

Use it
  • The original list order is not needed — save the allocation
  • Sorting is followed by more mutations on the same list
  • Very large lists where duplicating memory would hurt
Reach for something else
  • You need to keep the original order → sorted() copy
  • Sorting an iterable that is not a list → sorted()
  • One-liner in a chain (sort returns None, breaks the chain)
  • Concurrent reads of the list while sorting

Notes

Complexity
O(n log n) average and worst-case; O(n) for already-sorted or reverse-sorted input
Return
None; the list is mutated in place
CPython impl
Objects/listobject.c :: listsort_impl — Timsort algorithm
Memory
O(n) auxiliary space for the merge (Timsort), but no new list object
Thread-safe
Not safe under concurrent mutation of the same list

FAQ

It is a design signal — Python returns None from mutating methods to discourage the misleading `xs = xs.sort()` pattern and to remind callers that a copy would need `sorted()`.

History

1.0
list.sort has been part of the list type since Python 1.0.
2.4
Timsort adopted; key= parameter added; sort became stable.
3.0
cmp= parameter removed; key= is now the only way to customize comparison.