Python sort a list with sort, sorted, and keys

Python sorts a list two ways: list.sort() reorders the list in place, and sorted() returns a new sorted list while leaving the original unchanged. Both default to ascending order, take reverse=True for descending order, and accept a key= function for custom rules such as sorting by length or by a field. Reach for sort() when you want to change the list itself, and sorted() when the original order still matters.

Python Sort List Example For In-Place Ordering

Output:

Output will appear here...

Output:

['Ana', 'Mia', 'Zoe']

How This Example Works

  1. names starts in an unsorted order that matches how the data arrived.
  2. names.sort() reorders the same list in place using the default ascending order.
  3. sort() returns None, so the result is the mutated names list, shown 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. Use sorted() when you want a value back.

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 vs sorted: Which to Use

list.sort() reorders an existing list and returns None. sorted() leaves the original alone, returns a new list, and accepts any iterable, not just lists.

scores = [88, 72, 95]

ranked = sorted(scores)   # new list; scores is unchanged
print(ranked)             # [72, 88, 95]
print(scores)             # [88, 72, 95]
Use list.sort() when…Use sorted() when…
You own the list and want it reordered in place.You need a new list and must keep the original order.
You want to avoid allocating a second list.You are sorting any iterable, such as a tuple or dict view.
You do not need the return value, since it is None.You want the sorted result as a value to assign or chain.

Rule of thumb: sort() mutates the list, sorted() copies it; reach for sorted() when the original order still matters downstream.

Performance Considerations

Python’s sort uses Timsort, a stable, adaptive algorithm with O(n log n) worst-case time. Stability means equal elements keep their original order, so you can sort by a secondary key first and then the primary key for multi-level ordering. The key function runs once per item, so keep it cheap for large lists. list.sort() is the most memory-efficient choice because it rearranges in place, while sorted() trades extra memory to preserve the original.

More Patterns for Sorting Lists

Sort by a custom key, such as length.

words = ["box", "container", "bin"]
words.sort(key=len)
print(words)              # ['box', 'bin', 'container']

key takes a function that returns the value to compare, so the list orders by that result. The items themselves stay intact, only their order changes.

Sort a list of tuples or dicts by one field.

items = [("pen", 3), ("desk", 120), ("mug", 8)]
items.sort(key=lambda item: item[1])
print(items)              # [('pen', 3), ('mug', 8), ('desk', 120)]

A lambda pulls the field to sort on, here the price in position 1. The same pattern sorts a list of dictionaries with key=lambda row: row["age"].

Sort a dictionary by its values.

scores = {"Ana": 88, "Zoe": 72, "Mia": 95}
ranked = sorted(scores.items(), key=lambda kv: kv[1], reverse=True)
print(ranked)             # [('Mia', 95), ('Ana', 88), ('Zoe', 72)]

Sorting the .items() view gives tuples of key and value, and kv[1] reads each value. See the Python dict example for more on key/value access.

Sort in descending order.

scores = [88, 72, 95]
scores.sort(reverse=True)
print(scores)             # [95, 88, 72]

reverse=True works on both sort() and sorted(). Combine key and reverse to order by a field from highest to lowest, as in leaderboards.

When to Use Python sort list

  • Reorder a list you own and will reuse, using list.sort().
  • Keep the original intact by taking a new list from sorted().
  • Sort by a derived value, such as length or a tuple field, with a key function.
  • Order results from highest to lowest by adding reverse=True.
  • Avoid sorting mixed, non-comparable types directly; normalize them or pass a key first.