list.sort()

list.sort(*, key=None, reverse=False)
Returns: None · Updated March 14, 2026 · List Methods
lists methods mutation sorting

The .sort() method sorts the items of a list in-place, meaning it modifies the original list directly rather than creating a new one. The method returns None, so you never assign its result to a variable. This is different from the built-in sorted() function, which returns a new sorted list and leaves the original unchanged.

Syntax

list.sort(*, key=None, reverse=False)

Parameters

ParameterTypeDefaultDescription
keycallableNoneA function of one argument used to extract a comparison key from each element. If None, elements are compared directly.
reverseboolFalseIf True, the list is sorted in descending order instead of ascending.

Both parameters are keyword-only — you must use key= and reverse= syntax.

Examples

Basic usage — ascending order

Sort numbers in ascending order:

numbers = [5, 2, 8, 1, 9]
numbers.sort()
print(numbers)
# [1, 2, 5, 8, 9]

Sorting in descending order

Use reverse=True for descending order:

fruits = ["banana", "apple", "cherry"]
fruits.sort(reverse=True)
print(fruits)
# ['cherry', 'banana', 'apple']

Using the key parameter

Sort by a specific attribute or transformation:

# Sort strings by length
words = ["cat", "elephant", "dog", "hippopotamus"]
words.sort(key=len)
print(words)
# ['cat', 'dog', 'elephant', 'hippopotamus']

# Sort tuples by the second element
pairs = [(1, 'one'), (3, 'three'), (2, 'two')]
pairs.sort(key=lambda x: x[1])
print(pairs)
# [(1, 'one'), (2, 'two'), (3, 'three')]

Sorting with custom objects

Sort objects by a specific attribute:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __repr__(self):
        return f"Person({self.name}, {self.age})"

people = [
    Person("Alice", 30),
    Person("Bob", 25),
    Person("Charlie", 35)
]
people.sort(key=lambda p: p.age)
print(people)
# [Person(Bob, 25), Person(Alice, 30), Person(Charlie, 35)]

Case-insensitive sorting

Sort strings regardless of case:

words = ["Apple", "banana", "Cherry", "date"]
words.sort(key=str.lower)
print(words)
# ['Apple', 'banana', 'Cherry', 'date']

Important: .sort() returns None

The method mutates in-place and returns None:

numbers = [3, 1, 2]
result = numbers.sort()
print(result)
# None
print(numbers)
# [1, 2, 3]

Never do sorted_list = numbers.sort() — you’ll get None.

Common Patterns

Sorting by absolute value

numbers = [-5, 2, -8, 1, -3]
numbers.sort(key=abs)
print(numbers)
# [1, 2, -3, -5, -8]

Sorting in reverse without modifying original order

Use reverse=True to get descending order:

scores = [95, 87, 92, 88]
scores.sort(reverse=True)
print(scores)
# [95, 92, 88, 87]

Stable sorting

Python’s sort is stable — elements with equal keys maintain their relative order:

data = [('apple', 2), ('banana', 1), ('cherry', 2)]
data.sort(key=lambda x: x[1])
print(data)
# [('banana', 1), ('apple', 2), ('cherry', 2)]
# Note: 'apple' and 'cherry' kept their original order

Getting a sorted copy without modifying original

If you need both the original and sorted versions:

original = [3, 1, 2]
sorted_copy = original.copy()
sorted_copy.sort()
print(original)
# [3, 1, 2]
print(sorted_copy)
# [1, 2, 3]

Or use the sorted() built-in:

original = [3, 1, 2]
sorted_list = sorted(original)
print(original)
# [3, 1, 2]
print(sorted_list)
# [1, 2, 3]

Performance Note

  • .sort() uses Tim Sort — O(n log n) average and worst case
  • Performs an in-place sort with O(1) extra space (apart from the recursion stack)
  • Much faster than manual sorting algorithms in pure Python
import timeit

large_list = list(range(10000))
shuffled = large_list.copy()
random.shuffle(shuffled)

t = timeit.timeit(lambda: shuffled.copy().sort(), number=1000)
print(f"Time for 1000 sorts: {t:.4f}s")

See Also