How to Sort a List in Python with sorted()
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
sorted(<iterable>, key=<function>, reverse=<True or False>)
| Part | What 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
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))

Step 2: Run It
uv run python sorting.py
['daisy', 'dandelion', 'roses', 'tulips']
['tulips', 'daisy', 'roses', 'dandelion']
['tulips', 'roses', 'dandelion', 'daisy']
[1, 2, 3]
['Cherry', 'apple', 'banana']
['apple', 'banana', 'Cherry']

Line by line:
sorted(names)put the names in alphabetical order.namesis still in its old order:sorted()made a new list.reverse=Truegave the order from Z to A.- Numbers sort from smallest to largest.
Cherrycame first without a key, because Python compares letters by their code, and every capital letter comes before every small one.key=str.lowercompared lowercase copies of the words, soCherrycame afterbanana, where a reader expects it. The words themselves didn't change.
The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
print(names.sort()) | Prints None. .sort() sorts the list in place and returns nothing | Call names.sort() on its own line, or use sorted(names) for a new list |
| Sorting numbers that are stored as text | sorted(["10", "9", "100"]) gave ['10', '100', '9']: text is compared character by character | Add key=int: it gave ['9', '10', '100'] |
| Mixing numbers and text in one list | sorted([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
- sorted() — the built-in function.
- list.sort() — sorting a list in place.
- Sorting Techniques — the official sorting how-to.
