Skip to content
List Methods

sort()

Sort the elements of the list in ascending or custom order.

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

list.sort() sorts the list in place using Python's Timsort algorithm (stable O(n log n)).

You can customize the sorting logic with a key transformation function and sort in descending order with reverse=True.

Parameters

NameDescription
keyOptional one-argument function used to extract a comparison key from each element (e.g. len, str.lower).
reverseOptional boolean. If True, sorts the list in descending order (defaults to False).

Returns

None — the list is sorted in place.

Try it

Run it and change it
words = ["banana", "pie", "apple", "strawberry"]
words.sort(key=len)
print("Sorted by length:", words)
words.sort(reverse=True)
print("Alphabetical reverse:", words)

Worth knowing

  • sort() vs sorted(): list.sort() mutates the list in place and returns None. The built-in sorted(iterable) returns a brand new sorted list.
  • Stability: Python's sort is guaranteed to be stable — elements with equal keys maintain their original relative order.

Related entries