Python sort() vs sorted(): What Is the Difference?
list.sort() sorts a list in place and returns None, while sorted(iterable) leaves the original untouched and returns a new sorted list; sorted() also works on tuples, sets, strings and dictionaries.Python has two ways to sort, and choosing the right one depends on whether you still need the original order. sort() is a method that only lists have. sorted() is a built-in function that accepts any iterable and always gives back a fresh list.
Both accept the same key and reverse arguments, so everything you learn about one transfers to the other. The sorting lesson explains the arguments in depth, and the reference pages cover list.sort() specifically.
scores = [70, 95, 82]
print(sorted(scores))
print(scores)
scores.sort()
print(scores)sort() vs sorted() comparison table
| Aspect | `list.sort()` | `sorted()` |
|---|---|---|
| Kind | List method | Built-in function |
| Works on | Lists only | Any iterable: list, tuple, set, string, dict, generator |
| Changes the original? | Yes, in place | No |
| Returns | None | A new list |
| Parameters | key=None, reverse=False | iterable, key=None, reverse=False |
| Stable (keeps equal items in original order)? | Yes | Yes |
| Memory | No extra list is created | Creates a new list |
| Typical use | Sort a list you own and no longer need unsorted | Sort for display or keep the original order |
When to use list.sort()
Use sort() when you own the list and the unsorted order is no longer needed.
- A leaderboard that should stay sorted after each update.
- Large lists where avoiding a second copy saves memory.
Remember that it returns None, so write scores.sort() on its own line and do not assign the result.
When to use sorted()
Use sorted() in all other cases.
- You want to keep the original order for later.
- The data is not a list:
sorted("hello"),sorted(my_tuple)orsorted(my_set). Sorting a set this way is also how you print it in a predictable order. - You need a sorted result inside an expression or a function call, such as
for name in sorted(names):.
Sorting a dictionary with sorted(prices) sorts its keys; use sorted(prices.items(), key=...) to sort by value.
Sorting with key and reverse
Both tools take key, a function that returns the value to compare, and reverse=True for descending order. For example key=len sorts by length and key=str.lower ignores case. Sorting is stable, so items with equal keys keep their earlier relative order, which lets you sort in several passes. A lambda is a handy short key function for records, such as key=lambda s: s["marks"].
Examples you can run
Press Run, then edit the code and run it again.
In place vs a new list
sorted() leaves prices alone, while sort() rearranges it and returns None.
prices = [30, 10, 20]
new_prices = sorted(prices)
print(new_prices)
print(prices)
result = prices.sort()
print(result)
print(prices)Expected output[10, 20, 30]
[30, 10, 20]
None
[10, 20, 30]
sorted() works on any iterable
Strings, tuples and sets all produce a sorted list. reverse=True flips the order.
print(sorted("python"))
print(sorted((3, 1, 2), reverse=True))
print(sorted({"pear", "apple", "fig"}))Expected output['h', 'n', 'o', 'p', 't', 'y']
[3, 2, 1]
['apple', 'fig', 'pear']
Sort records by a key
The key function picks the marks, and the stable sort keeps equal marks in their original order.
students = [
{"name": "Asha", "marks": 88},
{"name": "Ravi", "marks": 92},
{"name": "Meena", "marks": 88},
]
ranked = sorted(students, key=lambda s: s["marks"], reverse=True)
for s in ranked:
print(s["name"], s["marks"])Expected outputRavi 92
Asha 88
Meena 88
Common mistakes
Assigning the result of sort()
✗ Wrong
scores = [3, 1, 2]
scores = scores.sort()
print(scores)✓ Right
scores = [3, 1, 2]
scores.sort()
print(scores)sort() returns None. Call it on its own line, or use scores = sorted(scores).
Calling sort() on a tuple or string
✗ Wrong
point = (3, 1, 2)
point.sort()✓ Right
point = (3, 1, 2)
print(sorted(point))Only lists have sort(). Use sorted() to get a sorted list from any iterable.
Sorting mixed types
✗ Wrong
data = [3, "2", 1]
print(sorted(data))✓ Right
data = [3, "2", 1]
print(sorted(data, key=int))Convert everything to one type first, or use a key function such as key=int.
Python sort() vs sorted(): What Is the Difference?: questions people ask
What is the difference between sort() and sorted() in Python?
sort() changes the list in place and returns None, while sorted() returns a new sorted list and leaves the original unchanged.
Is sorted() faster than sort()?
sort() is slightly faster and uses less memory because it does not build a second list, but both use the same algorithm (Timsort) and are O(n log n).
How do I sort in descending order in Python?
Pass reverse=True, as in sorted(scores, reverse=True) or scores.sort(reverse=True).
Keep learning
Step-by-step lessons:
Python SortingPython List MethodsPython Lambda Functions
Related:
Python List sort() MethodPython append() vs extend(): What Is the Difference?Python List vs Set: Differences and When to Use EachPython List reverse() Method
More in functions and methods:
Python append() vs extend(): What Is the Difference?Python remove() vs pop() vs del: What Is the Difference?Python print() vs return: What Is the Difference?Python str() vs repr(): What Is the Difference?Python Deep Copy vs Shallow Copy: What Is the Difference? See all →