When we need to sort a list in Python, there are two common tools: list.sort() and sorted(). Although both can sort, they differ fundamentally in modifying the original list and return values, which often confuses beginners. Today, we’ll clarify their usage with simple examples and clear comparisons!
1. list.sort(): In-place Sorting (Modifies the Original List)¶
list.sort() is a method of the list itself. It is called directly on the list, e.g., my_list.sort(). Its core features are: it modifies the original list directly and returns None (no return value).
Example:
# Define a list to be sorted
my_list = [3, 1, 4, 2]
# Call the sort() method
my_list.sort()
# Print the sorted list (original list has been modified)
print(my_list) # Output: [1, 2, 3, 4]
If you try to get the return value, it will be None:
result = my_list.sort()
print(result) # Output: None (sort() does not return a new list)
2. sorted(): Non-in-place Sorting (Creates a New List)¶
sorted() is a built-in function in Python. It is called by passing the list directly, e.g., sorted(my_list). Its core features are: it does not modify the original list but returns a new sorted list.
Example:
# Define a list to be sorted
my_list = [3, 1, 4, 2]
# Call sorted() to get a new sorted list
sorted_list = sorted(my_list)
# Original list remains unchanged
print(my_list) # Output: [3, 1, 4, 2]
# The new list is the sorted result
print(sorted_list) # Output: [1, 2, 3, 4]
Core Difference: In-place Modification vs. Preserving Original List¶
| Comparison Item | list.sort() (List Method) |
sorted() (Built-in Function) |
|---|---|---|
| Modifies Original List | Yes (directly changes the original list) | No (returns a new list without modifying the original) |
| Return Value | Returns None |
Returns the sorted new list |
| Use Case | When the original list is unnecessary | When the original list needs to be preserved, or sorting other iterables |
Advanced Parameters: reverse and key¶
Both sort() and sorted() support two parameters to control sorting details:
(1) reverse: Control Ascending/Descending Order¶
- Default:
reverse=False(ascending order, from smallest to largest). - Set to
reverse=Truefor descending order (from largest to smallest).
Example:
my_list = [3, 1, 4, 2]
my_list.sort(reverse=True) # Sort in descending order
print(my_list) # Output: [4, 3, 2, 1]
# sorted() also supports reverse
sorted_list = sorted(my_list, reverse=True)
print(sorted_list) # Output: [4, 3, 2, 1]
(2) key: Custom Sorting Rules¶
To sort based on special criteria (e.g., string length, a specific element of a tuple), use the key parameter.
Example (Sort by string length):
words = ["apple", "cat", "banana"]
sorted_words = sorted(words, key=lambda x: len(x)) # Sort by string length (ascending)
print(sorted_words) # Output: ['cat', 'apple', 'banana'] (lengths: 3 → 5 → 6)
Here, lambda x: len(x) is an “anonymous function” that extracts the length of each string as the sorting key.
How to Choose?¶
- Use
list.sort(): When you don’t need the original list anymore and want to modify it directly (saves memory by avoiding extra space). - Use
sorted(): When you need to preserve the original list or sort other iterables (e.g., tuples, strings).
Common Pitfalls¶
- Don’t assume
sorted()modifies the original list: The original list remains unchanged;sorted()only returns a new list. - Don’t try to assign
sort()to a variable: It returnsNone, so you cannot store the result in a variable. - Tuples cannot use
sort(): Tuples are immutable and have nosort()method. Usesorted()to convert them to a list for sorting.
Summary¶
list.sort(): Modifies the original list in-place, returnsNone. Suitable when the original list is unnecessary.sorted(): Does not modify the original list, returns a new sorted list. Suitable when the original list needs to be preserved or sorting other objects.
Choose the appropriate tool based on whether you need to preserve the original list!