Python List sort() Method
list.sort() is the built-in list method for sorting items in a list.
Use this page as a quick reference for what sort() does, how its main arguments work, and the beginner mistakes to avoid.
Quick example
numbers = [3, 1, 2]
numbers.sort()
print(numbers) # [1, 2, 3]
Use list.sort() to sort a list in place. It changes the original list and returns None.
What list.sort() does
list.sort():
- Sorts the items of a list in place
- Changes the original list
- Returns
None - Works for numbers and strings when items can be compared
This means the list itself is updated after you call the method.
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)
Output:
[1, 2, 5, 9]
If you are still learning how lists work, see the Python lists beginner guide.
Basic syntax
my_list.sort(key=None, reverse=False)
Main arguments:
keylets you sort by a custom rulereverse=Truesorts in descending order- If no arguments are given, sorting is ascending
Example:
numbers = [4, 1, 7, 2]
numbers.sort(reverse=True)
print(numbers)
Output:
[7, 4, 2, 1]
Sort numbers
Use sort() when you want to sort numbers in ascending order.
numbers = [10, 3, 8, 1]
numbers.sort()
print(numbers)
Output:
[1, 3, 8, 10]
Use reverse=True for descending order:
numbers = [10, 3, 8, 1]
numbers.sort(reverse=True)
print(numbers)
Output:
[10, 8, 3, 1]
The important thing to remember is that the original list is updated after the method call.
For a task-focused guide, read how to sort a list in Python.
Sort strings
Strings are sorted alphabetically by default.
words = ["banana", "apple", "cherry"]
words.sort()
print(words)
Output:
['apple', 'banana', 'cherry']
Uppercase and lowercase letters affect the result:
words = ["banana", "Apple", "cherry"]
words.sort()
print(words)
Output:
['Apple', 'banana', 'cherry']
If you want case-insensitive sorting, use key=str.lower:
words = ["banana", "Apple", "cherry"]
words.sort(key=str.lower)
print(words)
Output:
['Apple', 'banana', 'cherry']
The original spelling stays the same. Python only uses str.lower as the sorting rule.
Using the key argument
The key argument changes how each item is compared.
Common examples:
lento sort by lengthstr.lowerto sort strings without case problems
Sort words by length:
words = ["pear", "banana", "fig", "apple"]
words.sort(key=len)
print(words)
Output:
['fig', 'pear', 'apple', 'banana']
Sort strings without case sensitivity:
words = ["Bob", "alice", "Carol"]
words.sort(key=str.lower)
print(words)
Output:
['alice', 'Bob', 'Carol']
The key argument is especially useful when the default alphabetical or numeric order is not what you want.
list.sort() vs sorted()
list.sort() and sorted() both sort data, but they work differently.
list.sort():
- Changes the existing list
- Returns
None
sorted():
- Returns a new sorted list
- Leaves the original unchanged
Example:
numbers = [3, 1, 2]
new_list = sorted(numbers)
print(numbers)
print(new_list)
Output:
[3, 1, 2]
[1, 2, 3]
Use sorted() when you want to keep the original list unchanged.
You can learn more in Python sorted() explained and sorted() vs list.sort() explained.
Common mistakes
Here are the most common beginner mistakes with list.sort().
Assigning the result of sort()
This is a very common mistake:
numbers = [3, 1, 2]
result = numbers.sort()
print(result)
print(numbers)
Output:
None
[1, 2, 3]
sort() does not return the sorted list. It returns None.
If you want a new list, use sorted() instead.
Sorting mixed types
Python cannot always compare different types directly.
items = [1, "two", 3]
items.sort()
This raises a TypeError because Python cannot sort integers and strings together by default.
Forgetting that sort() only works on lists
sort() is a list method. It does not work directly on tuples or strings.
Wrong:
text = "python"
text.sort()
Strings do not have a sort() method.
If needed, convert the data first:
letters = list("python")
letters.sort()
print(letters)
Output:
['h', 'n', 'o', 'p', 't', 'y']
Not using key when custom sorting is needed
If the default order is not correct for your problem, you may need key.
For example, if you want to sort words by length, plain sort() will not do that:
words = ["banana", "fig", "apple"]
words.sort(key=len)
print(words)
Unexpected string order because of uppercase letters
This often surprises beginners:
words = ["zebra", "Apple", "monkey"]
words.sort()
print(words)
Output:
['Apple', 'monkey', 'zebra']
Use key=str.lower if you want case-insensitive sorting.
Useful debugging checks
If sorting is not working as expected, these quick checks can help:
print(my_list)
print(type(my_list))
print(my_list.sort())
print(sorted(my_list))
print([type(item) for item in my_list])
These checks help you confirm:
- What values are in the list
- Whether the object is really a list
- That
sort()returnsNone - Whether
sorted()works better for your case - Whether the list contains mixed data types
If your problem is caused by incompatible values, see a guide for fixing sorting-related TypeError problems.
FAQ
Does list.sort() return a new list?
No. It changes the original list and returns None.
What is the difference between sort() and sorted()?
sort() changes the list itself. sorted() creates and returns a new sorted list.
How do I sort in reverse order?
Use:
my_list.sort(reverse=True)
How do I sort strings without case problems?
Use:
my_list.sort(key=str.lower)
This sorts in a case-insensitive way.
Why do I get a TypeError when sorting?
Your list may contain values that Python cannot compare directly, such as strings and integers together.