Skip to main content

How to Sort a List in Python with sorted()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Lists keep their items in the order you added them. To show them in order, alphabetically or by size, pass the list to Python's built-in sorted(). It returns a new, sorted list and leaves the original alone. This guide sorts names forwards, backwards and without caring about capital letters on Python 3.15.2.

Syntax​

Syntax
sorted(<iterable>, key=<function>, reverse=<True or False>)
PartWhat it means
<iterable>What to sort: a list, the result of iterdir(), a dictionary's keys and so on
key=<function>Optional. A function that turns each item into the value to compare, such as str.lower
reverse=<True or False>Optional. True sorts from largest to smallest. The default is False

The built-in functions documentation says sorted() returns "a new sorted list from the items in iterable", and that key and reverse "must be specified as keyword arguments".

The Example​

A list of four flower names, sorted three ways, then two short lists that show numbers and capital letters.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

sorting.py
names = ["tulips", "daisy", "roses", "dandelion"]

print(sorted(names))
print(names)
print(sorted(names, reverse=True))
print(sorted([3, 1, 2]))
print(sorted(["banana", "Cherry", "apple"]))
print(sorted(["banana", "Cherry", "apple"], key=str.lower))

The sorting.py script in a macOS-style code window: four flower names sorted, the original list printed, the names sorted with reverse=True, the numbers 3, 1 and 2 sorted, and banana, Cherry and apple sorted without and with key=str.lower.

Step 2: Run It​

Terminal
uv run python sorting.py
Output
['daisy', 'dandelion', 'roses', 'tulips']
['tulips', 'daisy', 'roses', 'dandelion']
['tulips', 'roses', 'dandelion', 'daisy']
[1, 2, 3]
['Cherry', 'apple', 'banana']
['apple', 'banana', 'Cherry']

The terminal after uv run python sorting.py: the names in alphabetical order, the original list unchanged, the names in reverse order, the list 1, 2, 3, Cherry before apple and banana, and apple, banana, Cherry with key=str.lower.

Line by line:

  • sorted(names) put the names in alphabetical order.
  • names is still in its old order: sorted() made a new list.
  • reverse=True gave the order from Z to A.
  • Numbers sort from smallest to largest.
  • Cherry came first without a key, because Python compares letters by their code, and every capital letter comes before every small one.
  • key=str.lower compared lowercase copies of the words, so Cherry came after banana, where a reader expects it. The words themselves didn't change.

The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
print(names.sort())Prints None. .sort() sorts the list in place and returns nothingCall names.sort() on its own line, or use sorted(names) for a new list
Sorting numbers that are stored as textsorted(["10", "9", "100"]) gave ['10', '100', '9']: text is compared character by characterAdd key=int: it gave ['9', '10', '100']
Mixing numbers and text in one listsorted([10, "9"]) stopped with TypeError: '<' not supported between instances of 'str' and 'int'Keep one type per list, or give a key that turns every item into the same type

The list documentation explains why .sort() returns None: "To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance)."

FAQ​

sorted() or .sort()?​

sorted() works on anything you can loop over and keeps the original. .sort() exists only on lists and changes the list itself, which saves memory on a very large list.

Is the sort stable?​

Yes. The documentation: "The built-in sorted() function is guaranteed to be stable", which means items that compare equal keep their original order.

Conclusion​

sorted() returned a new list in alphabetical order and left the original as it was. reverse=True reversed the order, and key=str.lower sorted words without caring about capital letters.

Additional Resources​