Python
list
sorting
programming
coding-tips

What is the difference between sortedlist vs list.sort?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python gives you two main ways to sort values: the built-in sorted() function and the list method .sort(). They use the same sorting algorithm, but they differ in return value, input flexibility, and memory behavior.

sorted() returns a new list

sorted() accepts any iterable and produces a new list in sorted order. The original object is left unchanged.

python
1numbers = [5, 2, 9, 1]
2result = sorted(numbers)
3
4print(numbers)  # [5, 2, 9, 1]
5print(result)   # [1, 2, 5, 9]

This is a good choice when you need the original order later or when the input is not a list at all.

python
names = {"Mina", "Ava", "Luca"}
print(sorted(names))

A set has no .sort() method, but sorted() can still consume it because a set is iterable.

list.sort() modifies the list in place

.sort() is a method on list objects. It changes the existing list and returns None.

python
1numbers = [5, 2, 9, 1]
2returned = numbers.sort()
3
4print(numbers)   # [1, 2, 5, 9]
5print(returned)  # None

That None return value is deliberate. It prevents a common class of bugs where someone assumes the method creates a new list.

If you want to sort a large list and do not need a copy, .sort() is often the better choice because it avoids allocating a second list object.

Both support the same sorting options

Both sorted() and .sort() accept key and reverse. The key function transforms each element into the value used for comparison.

python
1people = [
2    {"name": "Ana", "age": 31},
3    {"name": "Ben", "age": 24},
4    {"name": "Cara", "age": 24},
5]
6
7by_age = sorted(people, key=lambda person: person["age"])
8print(by_age)

You can also sort descending:

python
scores = [72, 91, 85]
scores.sort(reverse=True)
print(scores)  # [91, 85, 72]

Stability matters

Python uses Timsort for both forms. One of its most useful properties is stability: when two items compare equal, their original relative order is preserved.

python
1records = [
2    ("build", "high"),
3    ("deploy", "low"),
4    ("test", "high"),
5]
6
7records.sort(key=lambda item: item[1])
8print(records)

The two items with priority high stay in their original order. That makes multi-step sorting practical. You can sort by a secondary key first, then by a primary key, and the earlier ordering is preserved within equal groups.

When to choose each one

Use sorted() when:

  • the input is not a list
  • you need a new list and must preserve the original object
  • you want to use the result directly in an expression

Use .sort() when:

  • you already have a list
  • mutating that list is acceptable
  • you want lower memory overhead on large inputs
python
files = ["report.txt", "app.log", "data.csv"]
for name in sorted(files):
    print(name)

That example reads naturally because sorted() returns a value immediately usable by the loop.

Common Pitfalls

The most common mistake is writing new_list = old_list.sort(). The result will be None, because .sort() mutates the list rather than returning it.

Another pitfall is assuming sorted() is only for lists. It works with any iterable, including tuples, sets, generators, and dictionary keys.

It is also easy to forget that sorting mixed, non-comparable types can fail in Python 3. A list containing integers and strings will usually raise TypeError unless you provide a key that normalizes them.

Summary

  • 'sorted() returns a new list and leaves the original data unchanged.'
  • '.sort() mutates an existing list and returns None.'
  • Both support key and reverse and use the same stable sorting algorithm.
  • 'sorted() works on any iterable, while .sort() only exists on lists.'
  • Choose based on whether you need a copy or an in-place update.

Course illustration
Course illustration

All Rights Reserved.