sorted() vs .sort(), reverse and Stability
16 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Reports need data in order: orders by amount, students by marks. Python gives two tools, sorted() and list.sort(). They differ in one thing that matters in real code: whether the original list survives.
sorted() builds a new list
sorted() is a built-in function that takes any iterable and returns a new list with its items in ascending order. The original is not changed.
A report needs orders from smallest to largest, but the original list must stay in arrival order. Call sorted and keep both lists.
amounts = [250, 40, 900, 120]ranked = sorted(amounts)print(ranked)print(amounts)Example 1 · sorted() accepts any iterable
tickets = {"T-204", "T-017", "T-950"}print(sorted(tickets))print(sorted("sort"))print(type(sorted(tickets)).__name__)['T-017', 'T-204', 'T-950'] ['o', 'r', 's', 't'] list
What this code does
- A set has no order, but sorted turns it into an ordered list.
- A string is an iterable of characters, so
sorted("sort")gives a list of letters. - The result is always a list, whatever you pass in.
ranked is a new list. amounts still holds the arrival order.
.sort() changes the list itself
list.sort() is a list method that reorders the list in place and returns None. It makes no new list, so it saves memory, but the old order is lost.
When nobody needs the old order, sort in place. A common bug is writing ranked = amounts.sort(). It stores None, not a list.
amounts = [250, 40, 900, 120]result = amounts.sort()print(result)print(amounts)Example 2 · Same list, same object after .sort()
totals = [310, 95, 410]before = id(totals)totals.sort()print(id(totals) == before)print(totals)True [95, 310, 410]
What this code does
idgives the identity of the object in memory.- It is unchanged, so
.sort()reordered the same list instead of building a new one.
Python returns None on purpose. It reminds you that the list was changed and no new list exists.
reverse=True for descending order
A leaderboard shows the highest mark first. Pass reverse=True to either sorted or sort. It is the only change needed.
marks = [72, 95, 88, 95, 60]print(sorted(marks, reverse=True))Example 3 · Capital letters sort first
print(sorted(["banana", "apple", "Cherry"]))print(sorted(["banana", "apple", "Cherry"], key=str.lower))['Cherry', 'apple', 'banana'] ['apple', 'banana', 'Cherry']
What this code does
- Python compares text by character code, and every capital letter comes before every lowercase letter.
key=str.lowercompares lowercase copies but still returns the original words.
Never sort and then flip with [::-1] when items can tie. The next section shows why.
Sorting is stable
Stable sort is a sort that keeps items with equal keys in their original relative order. Python's sort is always stable.
Names with the same length are tied. A stable sort never shuffles tied items. They stay in the order they arrived.
names = ["Ravi", "Asha", "Meena", "Kiran", "Diya"]print(sorted(names, key=len))Example 4 · reverse=True keeps ties in order
names = ["Ravi", "Asha", "Meena", "Kiran", "Diya"]print(sorted(names, key=len, reverse=True))print(sorted(names, key=len)[::-1])['Meena', 'Kiran', 'Ravi', 'Asha', 'Diya'] ['Kiran', 'Meena', 'Diya', 'Asha', 'Ravi']
What this code does
- With
reverse=True, tied names keep their arrival order: Ravi, Asha, Diya. - Flipping a finished list also flips the ties, so Diya comes before Ravi. The two lines differ.
key=len compares names by length. Ravi, Asha and Diya tie at 4 and keep their arrival order.
Next step
Check your understanding
11 quick questions