Home / Python / Reference / List methods

Python list sort() method

Sort a list in place.

Syntax

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

Changes the list itself and returns None. Use sorted(xs) for a new sorted list that leaves the original alone. key sorts by a computed value.

Parameters

keya function giving the value to sort by
reverseTrue for largest first

Return value

None

Example

Code

scores = [70, 92, 85]
scores.sort()
print(scores)
scores.sort(reverse=True)
print(scores)
people = [("Sita", 27), ("Leo", 31), ("Mia", 24)]
people.sort(key=lambda p: p[1])
print(people)

Output

[70, 85, 92]
[92, 85, 70]
[('Mia', 24), ('Sita', 27), ('Leo', 31)]
Good to know. xs = xs.sort() leaves you with None.

Related

Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.