Python Lists: Why sort() and sorted() Affect Your Data Differently
Sorting a list seems straightforward—until another variable unexpectedly changes, or your sorted result turns out to be None.
Understanding the difference between .sort() and sorted() becomes easier once you connect them to list references and copying.
Let’s explore that connection with a leaderboard.
1. Sorting through one name affects every alias
scores = [75, 90, 60]
original_scores = scores
scores.sort()
print(scores)
print(original_scores)
Output:
[60, 75, 90]
[60, 75, 90]
The assignment original_scores = scores does not preserve the original order. Both names refer to the same list.
The .sort() method changes that list in place, so both names show its new order.
2. Why does assigning .sort() produce None?
Consider this small change:
scores = [75, 90, 60]
ordered_scores = scores.sort()
print(scores)
print(ordered_scores)
Output:
[60, 75, 90]
None
The list was sorted successfully.
However, .sort() returns None. Assigning its return value does not give you the sorted list.
Use it as a separate statement when you intend to modify the existing list:
scores.sort()
3. Use sorted() to create a new outer list
If you need to preserve the original order, use sorted():
scores = [75, 90, 60]
ordered_scores = sorted(scores)
print(scores)
print(ordered_scores)
print(scores is ordered_scores)
Output:
[75, 90, 60]
[60, 75, 90]
False
The original list stays unchanged, and ordered_scores refers to a new list.
Here is the key distinction:
| Operation | Changes the input list? | Return value |
|---|---|---|
scores.sort() |
Yes, potentially changing its order | None |
sorted(scores) |
No | A new sorted list |
4. A new sorted list can still share its elements
Now let’s store player names and scores in dictionaries:
players = [
{"name": "Asha", "score": 75},
{"name": "Ben", "score": 90},
]
leaderboard = sorted(
players,
key=lambda player: player["score"],
reverse=True,
)
print([player["name"] for player in players])
print([player["name"] for player in leaderboard])
Output:
['Asha', 'Ben']
['Ben', 'Asha']
The key function tells Python to sort by each player’s score. reverse=True puts the highest score first.
The outer lists have different orders, but their elements are still the same dictionaries:
print(leaderboard[0] is players[1])
Output:
True
Changing Ben’s score through the leaderboard therefore changes the original data too:
leaderboard[0]["score"] = 95
print(players[1]["score"])
Output:
95
sorted() creates a new list; it does not recursively copy the objects inside it.
5. Create an independent leaderboard when needed
For this data structure, deep copying before sorting separates the player dictionaries:
from copy import deepcopy
players = [
{"name": "Asha", "score": 75},
{"name": "Ben", "score": 90},
]
leaderboard = sorted(
deepcopy(players),
key=lambda player: player["score"],
reverse=True,
)
leaderboard[0]["score"] = 95
print(players[1]["score"])
print(leaderboard[0]["score"])
Output:
90
95
Whether you need that independence depends on your goal.
If the leaderboard should display the same player records in another order, sharing them may be appropriate. If it should be a separate snapshot, copying the nested data may be necessary.
Separate ordering from copying
When sorting Python data, ask two questions:
- Should the original list’s order change?
- Should the elements themselves remain shared?
.sort() and sorted() address the ordering decision. Copying determines which mutable objects can change independently.
For further reading, see the official Python Sorting HOWTO.
Free PCEP practice test
Preparing for PCEP? Access my AM Academy free practice test here: https://www.udemy.com/course/python-pcep-30-03-certification-practice-tests-2026/?couponCode=FREE-COURSE-PCEP. I’m the creator of this independent preparation resource; it is not affiliated with or endorsed by the Python Institute.
Top comments (0)