# Sort a list of tuples by the second element in Python Last updated: Jun 29, 2022 Photo from Unsplash

## Sort a list of tuples by the second element in Python#

Use the `key` argument of the `sorted()` function to sort a list of tuples by the second element, e.g. `sorted_list = sorted(list_of_tuples, key=lambda t: t)`. The function will return a new list, sorted by the second tuple element.

main.py
```Copied!```list_of_tuples = [(1, 50), (1, 20),  (1, 30)]

# ✅ sort list of tuples by second element (ascending order)
sorted_list = sorted(
list_of_tuples,
key=lambda t: t
)

print(sorted_list)  # 👉️ [(1, 20), (1, 30), (1, 50)]

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

# ✅ sort list of tuples by second element (descending order)

sorted_list = sorted(
list_of_tuples,
key=lambda t: t,
reverse=True
)

print(sorted_list)  # 👉️ [(1, 50), (1, 30), (1, 20)]
``````

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, 50), (1, 20),  (1, 30)]

sorted_list = sorted(
list_of_tuples,
key=lambda t: t
)

print(sorted_list)  # 👉️ [(1, 20), (1, 30), (1, 50)]
``````
Python indexes are zero-based. The first item in a tuple has an index of `0`, the second an index of `1`, etc.

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

The example sorts the list of tuples by the second element in each tuple.

If you need to sort the list of tuples by the second element in descending order (greatest to lowest), set the `reverse` argument to `true` when calling `sorted()`.

main.py
```Copied!```list_of_tuples = [(1, 50), (1, 20),  (1, 30)]

sorted_list = sorted(
list_of_tuples,
key=lambda t: t,
reverse=True
)

print(sorted_list)  # 👉️ [(1, 50), (1, 30), (1, 20)]
``````
If the `reverse` argument is set to `True`, then the elements are sorted as if each comparison were reversed.

You can also 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, 50), (1, 20),  (1, 30)]

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

print(sorted_list)  # 👉️ [(1, 20), (1, 30), (1, 50)]
``````

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`.

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 to sort the list of tuples by the second element in place.

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

list_of_tuples = [(1, 50), (1, 20),  (1, 30)]

list_of_tuples.sort(key=itemgetter(1))

# 👇️ [(1, 20), (1, 30), (1, 50)]
print(list_of_tuples)
``````

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

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.