How to Sort a List of Tuples in Python

To sort a list of tuples in Python, call sorted(list_of_tuples) or list_of_tuples.sort(). Tuples compare element by element, so they sort by the first element, and ties are broken by the second. To sort by another position, pass key=itemgetter(i):

from operator import itemgetter
sorted(orders)                      # by first element
sorted(orders, key=itemgetter(1))   # by second element

The examples sort US orders by state, price and date. Each output below is what Python 3.12.5 printed.

You’ll see the default order, sort() vs sorted(), sorting by any element, reverse order, mixing ascending and descending, case-insensitive names, and tuples that contain None.

Sort a list of tuples by the first element

No key is needed. Python compares the first elements, and only looks at the second when the first ones match:

orders = [("TX", 129.99), ("CA", 54.50), ("NY", 230.25), ("CA", 19.99), ("TX", 89.00)]

print(sorted(orders))                 # by first element, ties broken by the second
print(orders[:2], "<- original unchanged")

orders.sort(reverse=True)             # in place, Z to A
print(orders)

Output:

[('CA', 19.99), ('CA', 54.5), ('NY', 230.25), ('TX', 89.0), ('TX', 129.99)]
[('TX', 129.99), ('CA', 54.5)] <- original unchanged
[('TX', 129.99), ('TX', 89.0), ('NY', 230.25), ('CA', 54.5), ('CA', 19.99)]
Command Prompt output of Python sorted ordering a list of state and price tuples by state with ties sorted by price
The two CA orders come first, cheapest first, then NY, then the two TX orders; sort(reverse=True) puts TX first.

sorted() returns a new list; .sort() changes the list and returns None. For one tuple on its own, see sorting a tuple in Python.

Sort a list of tuples by any element with key

key tells Python what to compare. itemgetter(i) is fast and readable; a lambda works when you need to compute something:

from operator import itemgetter

orders = [("TX", 129.99, "2026-10-02"), ("CA", 54.50, "2026-10-03"), ("NY", 230.25, "2026-10-01")]

print(sorted(orders, key=itemgetter(0)))          # first element only
print(sorted(orders, key=itemgetter(1)))          # by price
print(sorted(orders, key=lambda t: t[2]))         # by ISO date string
print(sorted(orders, key=itemgetter(1), reverse=True)[0])   # most expensive

Output:

[('CA', 54.5, '2026-10-03'), ('NY', 230.25, '2026-10-01'), ('TX', 129.99, '2026-10-02')]
[('CA', 54.5, '2026-10-03'), ('TX', 129.99, '2026-10-02'), ('NY', 230.25, '2026-10-01')]
[('NY', 230.25, '2026-10-01'), ('TX', 129.99, '2026-10-02'), ('CA', 54.5, '2026-10-03')]
('NY', 230.25, '2026-10-01')
Command Prompt output of Python sorting a list of order tuples by state, by price with itemgetter, and by date with a lambda
The orders come out by state, by price from $54.50 up, by date, and the most expensive order is NY at $230.25.

ISO date strings sort correctly as text. The second-element case has its own walkthrough in sorting by the second element in a tuple.

How do I sort tuples ascending by one element and descending by another?

reverse=True flips everything. For mixed directions, negate the numeric part of the key, or sort twice, because Python’s sort is stable:

from operator import itemgetter

orders = [("TX", 129.99), ("CA", 54.50), ("TX", 230.25), ("CA", 19.99), ("NY", 89.00)]

# state A-Z, then price high to low: negate the number
print(sorted(orders, key=lambda t: (t[0], -t[1])))

# same result with two stable passes (works for text too)
step1 = sorted(orders, key=itemgetter(1), reverse=True)
print(sorted(step1, key=itemgetter(0)))

Output:

[('CA', 54.5), ('CA', 19.99), ('NY', 89.0), ('TX', 230.25), ('TX', 129.99)]
[('CA', 54.5), ('CA', 19.99), ('NY', 89.0), ('TX', 230.25), ('TX', 129.99)]
Command Prompt output of Python sorting tuples by state A to Z and price high to low with a negated key and with two stable sorts
Both approaches give CA, NY, TX with each state’s highest price first, so the two methods agree.

Two passes also work for text, which you can’t negate: sort by the secondary key first, then the primary. Lists of lists sort the same way, as shown in sorting a list of lists in Python.

Sort tuples with text case-insensitively

Capital letters sort before lowercase ones, so mixed-case names come out oddly. Lowercase them in the key:

customers = [("olivia", 3), ("Emily", 5), ("james", 2), ("Zoe", 5)]

print(sorted(customers))                                      # capitals sort first
print(sorted(customers, key=lambda t: t[0].lower()))          # case-insensitive
print(sorted(customers, key=lambda t: (-t[1], t[0].lower()))) # most orders first, then name

Output:

[('Emily', 5), ('Zoe', 5), ('james', 2), ('olivia', 3)]
[('Emily', 5), ('james', 2), ('olivia', 3), ('Zoe', 5)]
[('Emily', 5), ('Zoe', 5), ('olivia', 3), ('james', 2)]

Zoe jumps ahead of james and olivia in the plain sort only because Z is a capital. The last line sorts by order count, highest first, then by name. Sorting plain strings the same way is covered in sorting a string in Python.

Fix TypeError when a tuple contains None

Python can’t compare None with a number, so a missing value breaks the sort. Put a flag first in the key so None values go last:

scores = [("Emily", 92), ("James", None), ("Olivia", 85)]

try:
    sorted(scores, key=lambda t: t[1])
except TypeError as err:
    print("TypeError:", err)

print(sorted(scores, key=lambda t: (t[1] is None, t[1] or 0)))   # None last

Output:

TypeError: '<' not supported between instances of 'NoneType' and 'int'
[('Olivia', 85), ('Emily', 92), ('James', None)]
Command Prompt output of Python TypeError not supported between NoneType and int when sorting tuples, fixed with a None-last key
The plain sort fails with a NoneType comparison TypeError; the flagged key puts Olivia and Emily first and James last.

(t[1] is None, ...) sorts False before True, so real values come first. Building these tuples from two lists is shown in adding tuples to lists in Python.

Ways to sort a list of tuples compared

GoalCode
First element, then the restsorted(lst)
In placelst.sort()
By element isorted(lst, key=itemgetter(i))
Largest firstsorted(lst, key=itemgetter(i), reverse=True)
A up, number downkey=lambda t: (t[0], -t[1])
None lastkey=lambda t: (t[1] is None, t[1] or 0)

Keep going with tuples and sorting:

Frequently asked questions

How do I sort a list of tuples in Python?

Use sorted(lst) to sort by the first element, or sorted(lst, key=itemgetter(i)) for another position.

How do I sort a list of tuples by the first element only?

sorted(lst, key=itemgetter(0)). Without a key, ties are also ordered by the next elements.

What’s the difference between sort() and sorted()?

sorted() returns a new list; list.sort() sorts in place and returns None.

How do I sort descending by one element and ascending by another?

Negate the numeric element in the key, like key=lambda t: (t[0], -t[1]), or sort twice.

Why do I get ‘<‘ not supported between NoneType and int?

A tuple holds None. Use a key that puts None last, such as (t[1] is None, t[1] or 0).

Where is sorting documented?

See the Sorting Techniques HOWTO in the Python docs.