# Sort a list of tuples in Python

Last updated: Jun 29, 2022

Check out my new book

## Sort a list of tuples in Python#

Use the `key` argument of the `sorted()` function to sort a list of tuples, e.g. `sorted_list = sorted(list_of_tuples, key=lambda t: t[1])`. The function will sort the list of tuples by the tuple element at the specified index.

main.py
```Copied!```list_of_tuples = [(1, 100), (2, 50), (3, 75)]

# ✅ sort list of tuples by element at index (ascending order)
sorted_list = sorted(list_of_tuples, key=lambda t: t[1])

print(sorted_list)  # 👉️ [(2, 50), (3, 75), (1, 100)]

# ----------------------------------------------------

# ✅ sort list of tuples by element at index in descending order

sorted_list_descending = sorted(
list_of_tuples,
key=lambda t: t[1],
reverse=True
)

# 👇️ [(1, 100), (3, 75), (2, 50)]
print(sorted_list_descending)
``````

The sorted function takes an iterable and returns a new sorted list from the items in the iterable.

The function takes an optional `key` argument that can be used to sort by different criteria.
main.py
```Copied!```list_of_tuples = [(1, 100), (2, 50), (3, 75)]

sorted_list = sorted(list_of_tuples, key=lambda t: t[1])

print(sorted_list)  # 👉️ [(2, 50), (3, 75), (1, 100)]
``````

The `key` argument can be set to a function that determines the sorting criteria.

The example sorts the list of tuples by the second item (index `1`) in each tuple.

If you need to sort the list of tuples in descending (greatest to lowest), set the `reverse` argument to `true` in the call to the `sorted()` function.

main.py
```Copied!```list_of_tuples = [(1, 100), (2, 50), (3, 75)]

sorted_list_descending = sorted(
list_of_tuples,
key=lambda t: t[1],
reverse=True
)

# 👇️ [(1, 100), (3, 75), (2, 50)]
print(sorted_list_descending)
``````

You can also use this approach to sort a list of tuples by multiple indexes.

main.py
```Copied!```list_of_tuples = [(1, 3,  100), (2, 3,  50), (3, 2, 75)]

# ✅ sort list of tuples by second and third elements
sorted_list = sorted(
list_of_tuples,
key=lambda t: (t[1], t[2])
)

print(sorted_list)  # 👉️ [(3, 2, 75), (2, 3, 50), (1, 3, 100)]
``````

The example sorts the list of tuples by the elements at index `1` and `2` in each tuple.

Since the second item in the third tuple is the lowest, it gets moved to the front.

The second items in the first and second tuples are equal (both `3`), so the third items get compared and since `50` is less than `100`, it takes second place.

An alternative to using a lambda function is to use the `operator.itemgetter()` method to specify the index you want to sort by.

main.py
```Copied!```from operator import itemgetter

list_of_tuples = [(1, 100), (2, 50), (3, 75)]

sorted_list = sorted(list_of_tuples, key=itemgetter(1))

print(sorted_list)  # 👉️ [(2, 50), (3, 75), (1, 100)]
``````

The operator.itemgetter method returns a callable object that fetches the item at the specified index.

For example, `x = itemgetter(1)` and then calling `x(my_tuple)`, returns `my_tuple[1]`.

You can also use this approach to sort by multiple indices.

main.py
```Copied!```from operator import itemgetter

list_of_tuples = [(1, 3,  100), (2, 3,  50), (3, 2, 75)]

# ✅ sort list of tuples by second and third elements
sorted_list = sorted(list_of_tuples, key=itemgetter(1, 2))

print(sorted_list)  # 👉️ [(3, 2, 75), (2, 3, 50), (1, 3, 100)]
``````

Using the `itemgetter` method is faster than using a lambda function, but it is also a bit more implicit.

Alternatively, you can use the `list.sort()` method.

Use the `key` argument of the `list.sort()` method to sort a list of tuples in place, e.g. `list_of_tuples.sort(key=lambda t: t[1])`. The `list.sort()` method will sort the list of tuples by the tuple element at the specified index.

main.py
```Copied!```list_of_tuples = [(1, 100), (2, 50), (3, 75)]

list_of_tuples.sort(key=lambda t: t[1])

print(list_of_tuples)  # 👉️ [(2, 50), (3, 75), (1, 100)]
``````

The list.sort method sorts the list in place and it uses only `<` comparisons between items.

The method takes the following 2 keyword-only arguments:

NameDescription
keya function that takes 1 argument and is used to extract a comparison key from each list element
reversea boolean value indicating whether each comparison should be reversed

The other keyword argument the `sort` method takes is `reverse`.

main.py
```Copied!```list_of_tuples = [(1, 100), (2, 50), (3, 75)]

list_of_tuples.sort(key=lambda t: t[1], reverse=True)

print(list_of_tuples)  # 👉️ [(1, 100), (3, 75), (2, 50)]
``````

Note that the `list.sort` method mutates the list in place and returns `None`.

You can use the `sorted()` function if you need a new list instance rather than an in-place mutation.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.