How to Sort a List in Python
Sorting a list is a common Python task. On this page, you will learn the simplest ways to sort a list in ascending order, descending order, and how to decide whether to change the original list or create a new sorted one.
Quick answer
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers) # [1, 2, 5, 9]
words = ["banana", "apple", "cherry"]
print(sorted(words)) # ['apple', 'banana', 'cherry']
Use list.sort() to change the original list. Use sorted() to return a new sorted list.
What this page helps you do
- Sort a list from smallest to largest
- Sort a list from largest to smallest
- Understand the difference between
sort()andsorted() - Sort numbers, strings, and simple custom cases
Use list.sort() when you want to change the original list
sort() is a list method. That means it belongs to list objects.
Important points:
sort()changes the list in place- It does not create a new list
- It is best when you no longer need the original order
Example:
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)
Output:
[1, 2, 5, 9]
This changes numbers itself.
If you want to learn more about this method, see the list.sort() method reference.
Use sorted() when you want a new sorted list
sorted() is a built-in Python function. It returns a new list and leaves the original data unchanged.
Important points:
sorted()works on lists and other iterable values- It returns a new list
- The original list stays unchanged
- It is useful when you need both the old order and the new order
Example:
numbers = [5, 2, 9, 1]
sorted_numbers = sorted(numbers)
print("Original:", numbers)
print("Sorted:", sorted_numbers)
Output:
Original: [5, 2, 9, 1]
Sorted: [1, 2, 5, 9]
This is the safer choice when you do not want to modify the original list.
For a deeper explanation, see sorted() explained.
Sort in descending order
By default, Python sorts in ascending order.
To sort from largest to smallest, use reverse=True.
With sort():
numbers = [5, 2, 9, 1]
numbers.sort(reverse=True)
print(numbers)
Output:
[9, 5, 2, 1]
With sorted():
numbers = [5, 2, 9, 1]
result = sorted(numbers, reverse=True)
print(result)
Output:
[9, 5, 2, 1]
reverse=True changes the normal ascending sort order.
If you specifically want to reverse list order without sorting, see how to reverse a list in Python.
Sort strings alphabetically
Strings sort alphabetically by default.
Example:
words = ["banana", "apple", "cherry"]
words.sort()
print(words)
Output:
['apple', 'banana', 'cherry']
Uppercase and lowercase matter
Python compares uppercase and lowercase characters differently, so the result may not look how you expect.
Example:
words = ["banana", "Apple", "cherry", "apple"]
print(sorted(words))
Output:
['Apple', 'apple', 'banana', 'cherry']
Sort strings without case differences
Use key=str.lower to compare lowercase versions of the strings.
words = ["banana", "Apple", "cherry", "apple"]
result = sorted(words, key=str.lower)
print(result)
Output:
['Apple', 'apple', 'banana', 'cherry']
This keeps the original string values, but sorts them as if they were lowercase.
Sort by a custom rule with key
Use key= when you want Python to sort items by a rule other than the default one.
Example: sort words by length
words = ["pear", "banana", "fig", "apple"]
result = sorted(words, key=len)
print(result)
Output:
['fig', 'pear', 'apple', 'banana']
Here, key=len tells Python to compare the lengths of the words.
Example: sort dictionaries by one value
students = [
{"name": "Mia", "score": 88},
{"name": "Leo", "score": 72},
{"name": "Ava", "score": 95}
]
result = sorted(students, key=lambda student: student["score"])
print(result)
Output:
[{'name': 'Leo', 'score': 72}, {'name': 'Mia', 'score': 88}, {'name': 'Ava', 'score': 95}]
This sorts the dictionaries by the score value.
If you want more practice with this, see how to sort a list of dictionaries in Python and lambda functions in Python explained.
When sorting may fail
Sorting may fail if the items in the list cannot be compared.
A common example is mixing numbers and strings in the same list.
items = [1, "2", 3]
print(sorted(items))
This usually raises an error in Python 3 because integers and strings cannot be directly compared during sorting.
To fix this:
- Make all values the same type
- Convert strings to numbers if needed
- Convert numbers to strings if you want text-based sorting
Example:
items = [1, "2", 3]
fixed = sorted([int(x) for x in items])
print(fixed)
Output:
[1, 2, 3]
If you are still learning lists, see Python lists explained for beginners.
How to choose the right method
Use this simple rule:
- Use
sort()for changing one existing list - Use
sorted()when you want a new result - Use
key=for custom sorting - Use
reverse=Truefor descending order
In short:
my_list.sort()→ changes the original listsorted(my_list)→ returns a new sorted list
Common mistakes
These are some common causes of sorting problems:
- Using
sort()and expecting it to return a new sorted list - Trying to sort a list with mixed types like
[1, '2', 3] - Forgetting
reverse=Truefor descending order - Not realizing uppercase letters affect string sorting
- Trying to use
sort()on something that is not a list
Useful checks:
print(my_list)
print(type(my_list))
print(sorted(my_list))
print(my_list.sort())
print([type(x) for x in my_list])
Why print(my_list.sort()) shows None
This confuses many beginners.
numbers = [3, 1, 2]
print(numbers.sort())
Output:
None
That happens because sort() changes the list in place and returns None.
Use this instead:
numbers = [3, 1, 2]
numbers.sort()
print(numbers)
Output:
[1, 2, 3]
FAQ
What is the difference between sort() and sorted() in Python?
sort() changes the original list. sorted() returns a new sorted list and leaves the original unchanged.
How do I sort a list in reverse order?
Use reverse=True, such as my_list.sort(reverse=True) or sorted(my_list, reverse=True).
Why does my sorted list print as None?
list.sort() returns None because it changes the list in place. Print the list after calling sort().
How do I sort strings without case differences causing problems?
Use key=str.lower so Python compares lowercase versions of the strings.
Can I sort a list by string length?
Yes. Use sorted(words, key=len) or words.sort(key=len).