Python Sort List

Learn how to sort a list in Python using list.sort() and sorted(). See examples for ascending order, descending order, strings, numbers, custom key functions, list of lists, tuples, dictionaries, None values, and common mistakes.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python Sort List

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.

python
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)
Output

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.

python
scores = [88, 92, 75, 90]
scores.sort()
print(scores)
Output

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.

python
scores = [88, 92, 75, 90]
sorted_scores = sorted(scores)

print(sorted_scores)
print(scores)
Output

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).

python
numbers = [5, 1, 4, 2, 3]
words = ["cherry", "apple", "banana"]

numbers.sort()
words.sort()

print(numbers)
print(words)
Output

Sort list in descending order

Use reverse=True for descending order. Do not confuse it with list.reverse().

  • reverse=True sorts first, then returns descending order.
  • list.reverse() only reverses the current order without sorting.
python
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)
Output

Sort list of strings alphabetically

Default string sorting is case-sensitive. Uppercase letters can sort before lowercase letters.

python
words = ["Banana", "apple", "Cherry"]

words.sort()
print(words)

words = ["Banana", "apple", "Cherry"]
words.sort(key=str.lower)
print(words)
Output

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.

python
words = ["apple", "fig", "banana", "kiwi"]
words.sort(key=len)
print(words)
Output

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.

python
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)
Output

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.

python
rows = [(2, "beta"), (1, "alpha"), (3, "gamma")]
sorted_rows = sorted(rows, key=lambda row: row[1])
print(sorted_rows)
Output

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.

python
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)
Output

Sort by multiple columns with a tuple key:

python
data = [[1, "b"], [1, "a"], [2, "z"]]
sorted_data = sorted(data, key=lambda row: (row[0], row[1]))
print(sorted_data)
Output

Sort list of tuples

Tuples sort by the first element by default. Sort by another position with a key function:

python
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)
Output

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"]:

python
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)
Output

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:

python
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)
Output

(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.

python
mixed = [3, "banana", 2.4, "apple"]

# Display-style sort only — converts everything to str
print(sorted(mixed, key=str))
Output

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:

python
queue = [5, 2, 8, 1]
display_order = sorted(queue)

print(display_order)
print(queue)
Output

This avoids the None return value from list.sort().


Common mistakes with Python list sorting

  • Assigning result = my_list.sort(). sort() returns None.
  • Using reverse() when you mean sort(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=len or key=str.lower is clearer.
  • Sorting list of lists by the wrong index.
  • Expecting case-insensitive string sort by default.
  • Using key=lambda x: -x instead of reverse=True for 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.


References


Frequently Asked Questions

1. What is the difference between sort() and sorted() in Python?

list.sort() changes the original list in place and returns None. sorted() returns a new sorted list and leaves the original unchanged.

2. How do you sort a list in descending order in Python?

Use my_list.sort(reverse=True) for in-place sorting or sorted(my_list, reverse=True) for a new list.

3. How do you sort a list of dictionaries by a key?

Use sorted(data, key=lambda item: item["field"]). Replace field with the dictionary key you want to sort by.

4. How do you sort a list of lists in Python?

Use sorted(data, key=lambda row: row[0]) to sort by the first column, or change the index for another column.

5. How do you sort a list with None values?

Use sorted(data, key=lambda x: (x is None, x)) to place None at the end while sorting the other values.

6. Is Python list sorting case-sensitive for strings?

Yes by default. Use key=str.lower or key=str.casefold for case-insensitive alphabetical sorting.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …