Python List Sort vs Sorted: In-Place vs New List
python list sort vs sorted: Understand the differences between list.sort() and sorted() in Python, including in-place vs new list behavior, performance, and when to us...
The question of python list sort vs sorted comes up frequently when developers need to order data in Python. The two approaches—the list.sort() method and the sorted() built-in function—serve similar purposes but behave differently in a key way: one modifies the list in place, and the other returns a new list.
The Core Difference: In-Place vs. New List
When you need to sort a list in Python, you have two standard options: the list.sort() method and the sorted() built-in function. The most important difference is that list.sort() modifies the list it is called on and returns None, while sorted() returns a new sorted list and leaves the original unchanged. This single distinction drives most of the decision-making in real code.
Syntax and Return Values
Here's a minimal example:
numbers = [3, 1, 2] result = numbers.sort() print(result) # None print(numbers) # [1, 2, 3]
With sorted():
numbers = [3, 1, 2] result = sorted(numbers) print(result) # [1, 2, 3] print(numbers) # [3, 1, 2]
The method returns None because it mutates the list in place. The function returns a new list, so you must assign it to a variable if you need the sorted result.
When to Use Each Approach
Use list.sort() when you no longer need the original order and want to save memory. This is common in data-processing pipelines where the list is a temporary working structure. Use sorted() when you need to preserve the original list for later use, or when you are sorting an iterable that is not a list, such as a tuple or a generator. sorted() accepts any iterable, while list.sort() is only available on lists.
| Aspect | list.sort() | sorted() |
|---|---|---|
| Modifies original list | Yes | No |
| Returns sorted list | None | New list |
| Works on any iterable | No (list only) | Yes |
| Memory usage | In-place, low | Creates new list, higher |
Performance and Memory Considerations
Both approaches use Timsort, Python's adaptive stable sorting algorithm, so the time complexity is O(n log n) in the average and worst cases. The practical performance difference comes from memory and copying. list.sort() sorts in place, so it does not allocate a new list. sorted() creates a new list and copies references from the original iterable into it. This means sorted() uses extra memory proportional to the size of the input. For very large lists, list.sort() can be more memory-efficient. Also, because list.sort() avoids the copy step, it can be slightly faster in practice, though the difference is often negligible for typical data sizes. The key point is that list.sort() is preferable when you want to minimize memory overhead and do not need the original list.
Sort Stability and Key Functions
Both list.sort() and sorted() are stable sorts, meaning that equal elements retain their original relative order. They also accept the same keyword arguments: key and reverse. For example:
words = ["banana", "apple", "cherry"] sorted_words = sorted(words, key=len) print(sorted_words) # ['apple', 'banana', 'cherry']
You can use a key function to sort by a derived property, and set reverse=True for descending order. The stability matters when you sort by multiple criteria in sequence.
Common Pitfalls and Edge Cases
A frequent mistake is assuming list.sort() returns the sorted list. Because it returns None, code like new_list = my_list.sort() will set new_list to None. Another pitfall is trying to call list.sort() on a tuple or other iterable; you must use sorted() for those. Also, be aware that sorting a list of mixed types (e.g., integers and strings) raises a TypeError in Python 3. If you need to handle mixed types, you must define a key function that provides a consistent ordering.
Choosing in Real Code
In practice, the choice often comes down to whether you need the original list. If you are building a list and then sorting it as a final step, list.sort() is idiomatic. If you are sorting data that arrives as a tuple or generator, sorted() is necessary. For example:
# In-place sort for a list you own data = [5, 2, 9, 1] data.sort() # Use the sorted list # New list from a tuple tuple_data = (5, 2, 9, 1) sorted_list = sorted(tuple_data)
This pattern keeps your intent clear and avoids accidental mutation of data you might need later.