List Methods

list.sort()

Reorders a list in place using less-than comparisons on the items or their extracted keys. The key callable is evaluated once per element in a successful sort. Equal-key items retain their input order, including with reverse=True. The result is None; use sorted() when a new list is needed. Comparison or key errors propagate, and a failed sort is not guaranteed to preserve the original order. Do not inspect or mutate a list while it is being sorted; CPython may detect mutation and raise ValueError. In-place does not mean zero auxiliary memory.

Formal Syntax & Structure

python syntaxpython
items.sort(*, key=None, reverse=False)

Parameters & Attributes

NameTypeDescription
keycallable or NoneOptional one-argument function producing comparison keys.
reverseboolSet True for descending order while maintaining stability.

Return Value

None

The original list changes; no new list is returned.

Working Example

Example Snippetpython
jobs = [("pack", 2), ("check", 1), ("ship", 2)]result = jobs.sort(key=lambda job: job[1], reverse=True)print(jobs)print(result)
Python Reference