ML Foundations
Python list.sort mutates; sorted returns a new list
Use list.sort when changing the existing list is intended. Use sorted when you need a separate ordered result or want to sort any iterable.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Choose mutation or a new result deliberately
Python provides two common sorting forms with different ownership behavior. `list.sort()` changes the list in place and returns `None`; the return value is intentionally not the sorted list. If you assign `result = values.sort()`, then `result` is `None` while `values` has been changed. [1]
The built-in `sorted(iterable)` returns a new list containing the ordered items. It accepts any iterable, not just an existing list, and supports `key` and `reverse`. Use it when the original sequence must remain unchanged or when a separate result is easier to reason about. [2]
Observe both results in a small example
The script starts with the list containing 3, 1 and 2, calls `.sort()`, and stores the return value. It then asks `sorted` for a descending copy of the already ascending list. The output shows the original list changed, the method returned `None`, and the second result is a distinct list. Assertions make each point explicit without relying on timing or implementation details. [1, 2]
If you need both versions, copy before sorting or use `sorted` for the second one. A shallow list copy creates a separate container but still refers to the same element objects; for numbers that distinction is immaterial, while mutable objects may still be shared. [1]
Executed with Python 3.12.14, standard library only; assertions passed.
"""Contrast list.sort mutation with sorted's new-list result."""
values = [3, 1, 2]
returned = values.sort()
new_descending = sorted(values, reverse=True)
print("list after sort:", values)
print("sort return:", returned)
print("sorted copy, reversed:", new_descending)
print("separate list:", new_descending is not values)
assert values == [1, 2, 3]
assert returned is None
assert new_descending == [3, 2, 1]
assert new_descending is not values
Recorded output
list after sort: [1, 2, 3] sort return: None sorted copy, reversed: [3, 2, 1] separate list: True
Avoid assigning the in-place result
A common bug is `values = values.sort()`: after the mutation, the variable is rebound to `None`. Keep the list reference when using `.sort()`, or write `values = sorted(values)` when rebinding to a newly produced list is intended. The syntax communicates whether ownership is changing. [1, 2]
Both operations use ordering comparisons and can accept `key` and `reverse` controls. This guide demonstrates only ordinary integers and the default ascending order plus a reversed copy. It makes no speed comparison; select the operation by the required mutation behavior, then benchmark only if a measured workload needs it. [1, 2]