Desk Clarity

Everyday document & browser help

Python List Becomes None After Sort: Separate Mutation From the Return Value

By ON Lab · Sources checked November 9, 2026

Keep an in-place sort as a statement. Use sorted when you need a returned sorted copy. Verify both the output and the source after choosing.

Reproduce the boundary in a disposable example

Assigning the result of list.sort loses the list reference.

values = [4, 1, 3]
alias = values
result = values.sort()
assert result is None
assert values == [1, 3, 4] and alias is values
source = [4, 1, 3]
ordered = sorted(source)
assert source == [4, 1, 3]
assert ordered == [1, 3, 4] and ordered is not source
rows = [{"rank": 2}, {"rank": 1}]
ordered_rows = sorted(rows, key=lambda row: row["rank"])
assert ordered_rows[0] is rows[1]
print("mutation, copy and element identity verified")

Read the result before changing application code

The alias still sees the reordered original. Conversely, the sorted list is a new outer container but its dictionary elements are shared. This matters when a caller expects the source order to remain stable or expects edits to a returned record to be isolated.

Choose the behavior your caller needs

For an owned working list, call values.sort() and then pass values onward. For a public helper that promises to preserve input order, return sorted(values). Add an assertion for the source order as well as the expected result order. If your variable is already None, find the earlier assignment rather than attempting another sort.

Scope and limits

This applies to built-in list sorting. It does not infer the return value of a custom collection’s sort method. Sorting a copy does not deep-copy its elements.

Related: compare the adjacent data or execution boundary.

The embedded example was executed in an isolated local Python process on November 9, 2026. It is a reproducible boundary check, not a test of your full application.

Sources