Python Function Reference

list.sort

Sort the list in place.

Python 3.13
list.sort(*, key=None, reverse=False)

Parameters

keyCallable | Noneoptional
A function whose result is used to compare items, such as len or str.lower.
reversebooloptional
If True, sorts in descending order.

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