Python gives you two main ways to sort a list: list.sort() and sorted(). Use list.sort() when you want to change the original list. Use sorted() when you want a new sorted list and need to keep the old order. To compare containers, see list vs set vs tuple vs dictionary.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Quick answer: sort a list in Python
numbers.sort() sorts the original list in place. sorted(numbers) returns a new sorted list and leaves the original unchanged.
numbers = [3, 1, 4, 1, 5]
numbers.sort()
print(numbers)
original = [3, 1, 4, 1, 5]
new_list = sorted(original)
print(new_list)
print(original)After the first sort, numbers is [1, 1, 3, 4, 5]. The sorted() call returns a new list while original stays [3, 1, 4, 1, 5].
Python sort list quick reference
| Task | Use |
|---|---|
| Sort list in place | my_list.sort() |
| Return a new sorted list | sorted(my_list) |
| Sort descending | my_list.sort(reverse=True) or sorted(my_list, reverse=True) |
| Sort strings alphabetically | words.sort() |
| Sort case-insensitively | words.sort(key=str.lower) |
| Sort by length | words.sort(key=len) |
| Sort list of lists by first item | sorted(data, key=lambda row: row[0]) |
| Sort list of lists by second item | sorted(data, key=lambda row: row[1]) |
| Sort list of tuples | sorted(data, key=lambda item: item[1]) |
| Sort list of dictionaries | sorted(data, key=lambda item: item["key"]) |
| Keep None at the end | sorted(data, key=lambda x: (x is None, x)) |
| Reverse without sorting | my_list.reverse() |
Sort a list in Python using sort()
list.sort() sorts the original list in place. It returns None, not the sorted list. It works only on lists.
scores = [88, 92, 75, 90]
scores.sort()
print(scores)A common mistake is writing result = my_list.sort(). That assigns None to result while the sorted data stays in my_list.
Sort a list in Python using sorted()
sorted() returns a new sorted list. It does not modify the original list, and it works with any iterable—not only lists.
scores = [88, 92, 75, 90]
sorted_scores = sorted(scores)
print(sorted_scores)
print(scores)Use sorted() when you need both the old order and a sorted copy.
sort() vs sorted() in Python
| Feature | list.sort() |
sorted() |
|---|---|---|
| Modifies original list | Yes | No |
| Returns new list | No, returns None |
Yes |
| Works with lists only | Yes | No, works with any iterable |
| Good when original order is not needed | Yes | No |
| Good when original order must stay | No | Yes |
Supports key and reverse |
Yes | Yes |
Sort list in ascending order
Ascending order is the default. Numbers sort numerically. Strings sort alphabetically (lexicographically).
numbers = [5, 1, 4, 2, 3]
words = ["cherry", "apple", "banana"]
numbers.sort()
words.sort()
print(numbers)
print(words)Sort list in descending order
Use reverse=True for descending order. Do not confuse it with list.reverse().
reverse=Truesorts first, then returns descending order.list.reverse()only reverses the current order without sorting.
numbers = [5, 1, 4, 2, 3]
numbers.sort(reverse=True)
print(numbers)
original = [5, 1, 4, 2, 3]
descending = sorted(original, reverse=True)
print(descending)
print(original)Sort list of strings alphabetically
Default string sorting is case-sensitive. Uppercase letters can sort before lowercase letters.
words = ["Banana", "apple", "Cherry"]
words.sort()
print(words)
words = ["Banana", "apple", "Cherry"]
words.sort(key=str.lower)
print(words)For stronger case-insensitive sorting, use key=str.casefold.
Sort list by length
Use key=len to sort by length. This works for strings, lists, tuples, and other sized objects.
words = ["apple", "fig", "banana", "kiwi"]
words.sort(key=len)
print(words)Shortest words appear first.
Sort list using key function
The key argument accepts a function used for comparison. Python calls the key function once for each item, which keeps sorting efficient.
words = ["apple", "Banana", "cherry"]
pairs = [(2, "b"), (1, "a"), (3, "c")]
words.sort(key=str.lower)
sorted_pairs = sorted(pairs, key=lambda item: item[0])
print(words)
print(sorted_pairs)Common key choices include len, str.lower, and lambda item: item[1].
Sort list using lambda
Use lambda when you need a small custom rule for tuples, dictionaries, or nested lists. Keep the expression readable.
rows = [(2, "beta"), (1, "alpha"), (3, "gamma")]
sorted_rows = sorted(rows, key=lambda row: row[1])
print(sorted_rows)Do not use lambda for simple descending numeric sorts when reverse=True is clearer.
Sort list of lists in Python
Python sorts nested lists by the first element by default. Use key=lambda row: row[index] to sort by a specific column.
data = [[3, "c"], [1, "a"], [2, "b"]]
by_first = sorted(data, key=lambda row: row[0])
by_second = sorted(data, key=lambda row: row[1])
print(by_first)
print(by_second)Sort by multiple columns with a tuple key:
data = [[1, "b"], [1, "a"], [2, "z"]]
sorted_data = sorted(data, key=lambda row: (row[0], row[1]))
print(sorted_data)Sort list of tuples
Tuples sort by the first element by default. Sort by another position with a key function:
pairs = [(1, "zebra"), (2, "apple"), (1, "alpha")]
by_second = sorted(pairs, key=lambda item: item[1])
by_both = sorted(pairs, key=lambda item: (item[0], item[1]))
print(by_second)
print(by_both)Use (item[1], item[0]) when you want to break ties by key after sorting by value.
Sort list of dictionaries
Sort by a dictionary field with key=lambda item: item["field"]:
students = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 20},
{"name": "Carol", "age": 25},
]
by_age = sorted(students, key=lambda item: item["age"])
by_name = sorted(students, key=lambda item: item["name"].lower())
print(by_age)
print(by_name)This pattern fits students by age, products by price, and users by name. For sorting a single dictionary’s keys or values, see sort dictionary by key and sort dictionary by value.
Sort list with None values
A list that mixes None with numbers cannot sort directly because None is not comparable to integers. Use a key function to control placement:
data = [3, None, 2, 1]
sorted_end = sorted(data, key=lambda x: (x is None, x))
print(sorted_end)
sorted_start = sorted(data, key=lambda x: (x is not None, x))
print(sorted_start)(x is None, x) puts None at the end. (x is not None, x) puts None at the start.
Sort mixed-type list
Python 3 does not sort incompatible mixed types by default. A list like [1, "a", 2] raises TypeError.
mixed = [3, "banana", 2.4, "apple"]
# Display-style sort only — converts everything to str
print(sorted(mixed, key=str))Normalize data before sorting when you need meaningful order, not just string representation.
Sort list without changing original
Use sorted() when the original order must stay intact:
queue = [5, 2, 8, 1]
display_order = sorted(queue)
print(display_order)
print(queue)This avoids the None return value from list.sort().
Common mistakes with Python list sorting
- Assigning
result = my_list.sort().sort()returnsNone. - Using
reverse()when you meansort(reverse=True).reverse()does not sort. - Forgetting that
sorted()returns a new list. - Forgetting that
sort()changes the original list. - Sorting mixed strings and numbers without a
key. - Using lambda when
key=lenorkey=str.loweris clearer. - Sorting list of lists by the wrong index.
- Expecting case-insensitive string sort by default.
- Using
key=lambda x: -xinstead ofreverse=Truefor simple descending numeric sort. - Assuming duplicate values disappear. Only order changes; items remain in the list.
Summary
Use list.sort() for in-place sorting when you do not need the old order. Use sorted() when you want a new sorted list. Add reverse=True for descending order, and use key= for custom rules such as length, lowercase strings, or fields inside tuples and dictionaries. Use lambda for nested lists and dictionary rows when a named function would be heavier than needed. Normalize mixed data before sorting, and handle None with a tuple key when it appears in numeric lists.

