Python list.sort() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
list method

Definition

sort() rearranges the items into ascending order, comparing them with <. It works in place and returns None; no new list is created.

key=function sorts by function(item) instead of the item itself, and reverse=True sorts in descending order. Both are keyword-only. The sort is stable: items whose keys are equal keep their original order.

list.sort() is an instance method of the built-in list type: call it on a list with dot notation, such as scores.sort(...). No import is needed.

Syntax

Call it with no arguments for ascending order, or name key and reverse explicitly.

python
lst.sort()
lst.sort(reverse=True)
lst.sort(key=len)
lst.sort(key=str.lower, reverse=True)
CallWhat it does
[3, 1, 2].sort()Returns None; the list becomes [1, 2, 3].
["b", "A", "a"].sort()Returns None; the list becomes ['A', 'a', 'b']: uppercase letters sort first.
[1, "2"].sort()Raises TypeError: an int and a str cannot be compared.

To keep the original order and get a sorted copy, use sorted(), which also works on tuples, strings, dicts, and any other iterable. lst.sort(len) raises TypeError; the key must be passed as key=len.

Method signatures
python
list.sort(*, key=None, reverse=False)
# returns: None; the list is now sorted

Parameters

ParameterDescription
keyOptional, keyword-only. A function of one argument, such as len, str.lower, or a lambda; it is called once per item and the results are compared. Default None compares the items directly.
reverseOptional, keyword-only. True sorts from largest to smallest. Equal items still keep their original order.

Return Value

Return ValueDescription
NoneAlways None. The list itself is now in sorted order.

Python Examples

Example 1 — Rank Exam Scores and Show the Top Three

Sort in place, see that the return value is None, then sort high to low and slice the top three.

python
scores = [88, 92, 75, 92, 60]

ranked = scores.sort()
print(ranked)
print(scores)

scores.sort(reverse=True)
print("top 3:", scores[:3])
print(sorted([5, 3, 9]), sorted("bca"))

How It Works

  1. The classic trap. ranked = scores.sort() sorts the list but stores None in ranked.
  2. Sorted in place. scores itself is now ascending, with the two 92s side by side.
  3. reverse=True. Sorting again with reverse=True puts the highest scores first, and the slice [:3] takes the top three.
  4. sorted() returns a list. The built-in returns a new sorted list, and on a string it gives a list of characters.

Example 2 — Alphabetize Product Names and Group Words by Length

Default order puts capitals first; key=str.lower fixes that, and key=len shows that the sort is stable.

python
fruits = ["banana", "Cherry", "apple", "Date"]

fruits.sort()
print(fruits)

fruits.sort(key=str.lower)
print(fruits)

words = ["kiwi", "fig", "plum", "pear", "lime", "yam"]
words.sort(key=len)
print(words)

How It Works

  1. Code point order. Uppercase "C" and "D" have smaller code points than any lowercase letter, so Cherry and Date come before apple.
  2. Case-insensitive key. key=str.lower compares lowercase versions while keeping the original spelling in the list.
  3. Sort by length. key=len puts the 3-letter words before the 4-letter ones.
  4. Stable ties. fig stays before yam, and kiwi, plum, pear, lime keep their original order, because their keys are equal.

Example 3 — Order Player Records by Points and Team

Sort dicts with a lambda, combine two keys with a tuple, and recover from a mixed int/str list.

python
players = [
    {"name": "Ivy", "team": "red", "points": 30},
    {"name": "Bo", "team": "blue", "points": 45},
    {"name": "Kai", "team": "red", "points": 45},
    {"name": "Ada", "team": "blue", "points": 30},
]

players.sort(key=lambda p: p["points"], reverse=True)
print([p["name"] for p in players])

players.sort(key=lambda p: (p["team"], -p["points"]))
print([(p["team"], p["name"]) for p in players])

mixed = [3, "10", 7]
try:
    mixed.sort()
except TypeError:
    print("cannot compare int and str; use key=int")
mixed.sort(key=int)
print(mixed)

How It Works

  1. Dicts need a key. Dicts cannot be compared with <, so the lambda picks the points to sort by.
  2. reverse keeps ties stable. Bo and Kai both have 45 and stay in their original order, as do Ivy and Ada with 30.
  3. Tuple keys. (team, -points) sorts by team name first, then by points from high to low inside each team.
  4. Mixed types fail. Comparing 3 with "10" raises TypeError; key=int compares them as numbers while the list keeps "10" as a string.

Frequently Asked Questions

It sorts the list in place, and Python returns None from in-place methods so you do not confuse the result with a new list. Use sorted(lst) when you want a new list back.
lst.sort() changes the list itself and only exists on lists. sorted(iterable) returns a new list, leaves the input alone, and works on any iterable.
Pass key=str.lower, as in names.sort(key=str.lower). Without it, every uppercase ASCII letter sorts before every lowercase one.
Yes. Items with equal keys always keep their original relative order, even with reverse=True, so you can sort by one key and then by another.
The list holds values that cannot be compared with <, such as ints and strings or dicts. Convert them first or pass a key that returns comparable values, for example key=int or key=str.

Back to the method index

See all 11 list method guides grouped by what they do.

Method Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful