Python sort list items in-place to order items
Python sort list arranges a list in place using the default order or a custom key. Use it when the list must be reordered to match your logic for later use. Important! In-place sorting updates the original list instead of creating a new one.
Python Sort List Example For In-Place Ordering
Output:
Output will appear here...
Output:
['Ana', 'Mia', 'Zoe']
How This Example Works
namesstarts in an unsorted order that matches how the data arrived.names.sort()reorders the same list in place, so every later use sees the new order.- The output shows the list now sorted alphabetically.
Common Mistakes
Mistake 1: Expecting sort() to return a new list.
scores = [88, 92, 75]
new_scores = scores.sort()
print(new_scores)
scores = [88, 92, 75]
scores.sort()
print(scores)
Why it happens: list.sort() always returns None, because it mutates the list you already have.
Mistake 2: Sorting mixed types in Python 3.
items = [3, "10", 2]
items.sort()
items = ["3", "10", "2"]
items.sort(key=int)
print(items)
Why it happens: numbers and strings are not directly comparable, so you must normalize types or provide a key.
Mistake 3: Forgetting that string sorting is case-sensitive.
names = ["ana", "Mia", "zoe"]
names.sort()
print(names)
names = ["ana", "Mia", "zoe"]
names.sort(key=str.lower)
print(names)
Why it happens: uppercase letters sort before lowercase by default, which can feel out of order for names.
sort list vs sorted: Which to Use
Use list.sort() when… | Use sorted() when… |
|---|---|
| You already have a list and want to reorder it in place. | You need a new list and want to keep the original order intact. |
| You want the most memory-efficient option. | You are sorting any iterable, not just a list. |
You are fine with the method returning None. | You want the sorted data as a new value. |
Rule of thumb: reach for list.sort() when the same list should change; use sorted() when you need a separate sorted result.
Performance Considerations
list.sort() is memory-friendly because it rearranges the existing list instead of allocating a new one. If you do not need the original order, it is usually the fastest and most memory-efficient option. The key function is called once per item, so keep that function simple for large lists. If you need to keep the original list untouched, sorted() trades extra memory for that safety.
More Patterns for Sorting Lists
Sort by a derived key, like length.
labels = ["box", "container", "bin"]
labels.sort(key=len)
print(labels)
This is useful when the display rule is not alphabetical and you want a clear ordering by size. It keeps the list in place while applying a custom rule.
Sort descending for leaderboards.
scores = [88, 92, 75]
scores.sort(reverse=True)
print(scores)
Descending order is common for top-N displays, and reverse=True keeps the code simple.
The list is still sorted in place, just in the opposite direction.
When to Use Python sort list
- Reorder a list that will be reused in multiple places afterward.
- Keep memory usage low by avoiding creation of a second list.
- Apply a
keyfunction when natural order is not what you need. - Avoid it when you must preserve the original order; use
sorted()or copy the list first. - Avoid it for non-list iterables unless you explicitly convert them to a list.