Python Function Reference

list.sort

Sort the list in place.

Python 3.13
list.sort(*, key=None, reverse=False)
  • =default value
  • *arguments after it by name only
key
A function whose result is used to compare items, such as len or str.lower; without it, the items themselves are compared.
Callable | NoneOptional
reverse
If True, sorts in descending order.
boolOptional

Return value

Nothing (None); the list is sorted in place.

Examples

Sorting a list in place

Python 3.13
a = [3, 1, 2]
a.sort()
print(a)
[1, 2, 3]

Case-insensitive sorting

Python 3.13
words = ['banana', 'Kiwi', 'apple']
words.sort(key=str.lower)
print(words)
['apple', 'banana', 'Kiwi']

Details

A common mistake is a = a.sort(): the method returns None, and the list is lost. To keep the original order, use sorted.

See also

Where to learn