Python Tutorial

Python List sort() Method with Examples (reverse, key, sorted vs sort)

TGTeam GyaniPanditNovember 25, 20263 min read

The sort() method arranges the items of a Python list in order, from smallest to largest by default. It changes the list in place and returns None. With its two optional arguments, reverse and key, you can sort in descending order, ignore capital letters, sort by length, or sort records by any field you choose.

Syntax

list.sort(key=None, reverse=False)
  • reverse: set to True for descending order.
  • key: a function that tells Python what to compare for each item.

Sort in Ascending Order

nums = [29, 4, 42, 5, 31, 22, 39]
nums.sort()
print(nums)

Output:

[4, 5, 22, 29, 31, 39, 42]
Python list sort method example

Sort in Descending Order

nums = [29, 4, 42, 5, 31, 22, 39]
nums.sort(reverse=True)
print(nums)

Output:

[42, 39, 31, 29, 22, 5, 4]

Sorting Strings (and the Capital-Letter Surprise)

words = ['banana', 'apple', 'Cherry', 'date']
words.sort()
print(words)

Output:

['Cherry', 'apple', 'banana', 'date']

“Cherry” comes first because Python compares characters by their Unicode values, and all capital letters come before small letters. To sort alphabetically regardless of case, use key=str.lower:

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

Output:

['apple', 'banana', 'Cherry', 'date']

Sort by Length with key=len

words = ['apple', 'banana', 'Cherry', 'date']
words.sort(key=len)
print(words)

Output:

['date', 'apple', 'banana', 'Cherry']

“banana” and “Cherry” both have 6 letters, so they keep their original order. Python’s sort is stable: items that compare equal stay in the order they were in.

Sort a List of Tuples or Records

To sort students by marks, highest first, use a lambda as the key:

students = [('Riya', 88), ('Aman', 95), ('Kabir', 88), ('Zoya', 72)]
students.sort(key=lambda s: s[1], reverse=True)
print(students)

Output:

[('Aman', 95), ('Riya', 88), ('Kabir', 88), ('Zoya', 72)]

Riya stays ahead of Kabir because both have 88 and Riya came first in the original list.

sort() vs sorted()

list.sort() sorted(iterable)
Changes the original? Yes, in place No, returns a new list
Returns None A new sorted list
Works on Lists only Any iterable: list, tuple, string, set, dictionary keys
y = [3, 1, 2]
z = sorted(y)
print(y, z)              # [3, 1, 2] [1, 2, 3]

print(sorted((5, 2, 9))) # [2, 5, 9]
Python list sort reverse example

Common Mistakes

1. Saving the result of sort()

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

Output:

None [1, 2, 3]

sort() returns None. Use the list itself after sorting, or use sorted() if you need a new list.

2. Sorting mixed types

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

Python 3 cannot compare numbers with strings. Convert the items to one type first, or pass a key such as key=str.

Frequently Asked Questions

Does sort() return anything?

No, it returns None and sorts the list in place.

How do I sort a list in descending order in Python?

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

Is Python’s sort stable?

Yes. Items with equal keys keep their original relative order.

Related: Python structural pattern matching

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses