Chapters
Python114 chapters

ReferenceList methods

list.sort()

Reorders the list in place.

Signature

items.sort(key=None, reverse=False)

Parameters

NameMeans
keycalled on each item to decide the ordering
reverselargest first

Returns

None. Use sorted() when you want a new list.

Example

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

words = ["banana", "Apple"]
words.sort(key=str.lower)
print(words)

Output

None
[1, 2, 3]
['Apple', 'banana']

See also