ReferenceList methods

Python List sort() Method

Short answer
list.sort(key=None, reverse=False) sorts the list in place in ascending order, is stable, and returns None; use sorted() if you need a new list.

sort() rearranges the items of an existing list so they are in order, for example scores from low to high or names from A to Z. Because it modifies the list itself and returns None, it is easy to misuse: ranked = scores.sort() leaves ranked as None.

For the bigger picture, including sorted() on other collections, read sorting in Python.

Quick facts
Syntaxmy_list.sort(key=None, reverse=False)
Parameterskey (keyword-only): function applied to each item to get the sort value; reverse (keyword-only): True for descending
ReturnsNone (the list is modified in place)
Time complexityO(n log n); stable

How list.sort() orders a list in place

Numbers sort numerically and strings sort by character code, so uppercase letters come before lowercase ones. The sort is stable: items that compare equal keep their original relative order, which is what makes multi-step sorting work.

Python
scores = [72, 95, 60, 88]
scores.sort()
print(scores)
scores.sort(reverse=True)
print(scores)

Sorting with the key parameter

key takes a function that is called once per item, and the items are ordered by the values it returns. Use key=len to sort by length, key=str.lower for case-insensitive text, or a lambda to pick a field out of a record. Both key and reverse must be passed by name.

list.sort() vs sorted()

Choose by whether you want to keep the original order.

Aspect`my_list.sort()``sorted(iterable)`
Works onLists onlyAny iterable (tuple, set, dict keys, ...)
Changes the originalYes, in placeNo
ReturnsNoneA new list
Argumentskey, reversekey, reverse
MemoryNo extra listMakes a new list

Mixed types and sorting errors

Python 3 refuses to compare unrelated types, so a list that mixes numbers and strings raises a TypeError. Convert everything to one type first, or provide a key that maps each item to a comparable value. If an error happens halfway through, the list may be left partly rearranged, so validate your data beforehand.

Examples you can run

Press Run, then edit the code and run it again.

Ascending and descending

Sort a list of prices both ways.

Ascending and descending
prices = [19.5, 5.0, 12.25, 8.0]
prices.sort()
print(prices)
prices.sort(reverse=True)
print(prices)
Expected output[5.0, 8.0, 12.25, 19.5]
[19.5, 12.25, 8.0, 5.0]

Sort by a key function

Sort words by length, then case-insensitively.

Sort by a key function
words = ["pear", "Fig", "banana", "apple"]
words.sort(key=len)
print(words)
words.sort(key=str.lower)
print(words)
Expected output['Fig', 'pear', 'apple', 'banana']
['apple', 'banana', 'Fig', 'pear']

Sort records by one field

Sort students by score, highest first, using a lambda.

Sort records by one field
students = [("Asha", 82), ("Dev", 91), ("Meera", 75)]
students.sort(key=lambda s: s[1], reverse=True)
print(students)
Expected output[('Dev', 91), ('Asha', 82), ('Meera', 75)]

Common mistakes

Assigning the result of sort()

✗ Wrong

scores = [3, 1, 2]
scores = scores.sort()
print(scores)
None

✓ Right

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

sort() returns None. Call it alone, or use scores = sorted(scores).

Mixing numbers and strings

✗ Wrong

data = [3, "2", 1]
data.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

✓ Right

data = [3, "2", 1]
data.sort(key=int)
print(data)

Convert to one type: data.sort(key=int).

Passing key positionally

✗ Wrong

words = ["bb", "a"]
words.sort(len)
TypeError: sort() takes no positional arguments

✓ Right

words = ["bb", "a"]
words.sort(key=len)
print(words)

Name the argument: words.sort(key=len).

Python List sort() Method: questions people ask

What is the difference between sort() and sorted() in Python?

sort() sorts a list in place and returns None, while sorted() accepts any iterable and returns a new sorted list, leaving the original unchanged.

How do I sort a list in descending order?

Pass reverse=True: my_list.sort(reverse=True), or use sorted(my_list, reverse=True) for a new list.

Is Python's list sort stable?

Yes. Items that compare equal keep their original order, so you can sort by a secondary key first and then by the main key.

Keep learning

Step-by-step lessons:
Python SortingPython ListsPython Lambda Functions

Related:
Python List reverse() MethodPython List MethodsPython List copy() MethodPython List index() Method

More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List remove() MethodPython List pop() MethodPython List clear() MethodPython List index() MethodPython List count() Method See all →