Sort a list of tuples by the Nth tuple element in Python

Borislav Hadzhiev

Last updated: Jun 29, 2022

Photo from Unsplash

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

main.py

`list_of_tuples = [(2, 44), (3, 22), (1, 33)] # ✅ sort list of tuples by first element (ascending order) sorted_list = sorted( list_of_tuples, key=lambda t: t[0] ) # 👇️ [(1, 33), (2, 44), (3, 22)] print(sorted_list) # ---------------------------------------------------------- # ✅ sort list of tuples by first element (descending order) sorted_list_desc = sorted( list_of_tuples, key=lambda t: t[0], reverse=True ) # 👇️ [(3, 22), (2, 44), (1, 33)] print(sorted_list_desc)`

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

`list_of_tuples = [(2, 44), (3, 22), (1, 33)] sorted_list = sorted(list_of_tuples, key=lambda t: t[0]) # 👇️ [(1, 33), (2, 44), (3, 22)] print(sorted_list)`

The example sorts the list of tuples by the first (index `0`

) element in each
tuple.

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.

If you need to sort the list of tuples by the Nth element in descending order
(greatest to lowest), set the `reverse`

argument to `true`

when calling
`sorted()`

.

main.py

`list_of_tuples = [(2, 44), (3, 22), (1, 33)] sorted_list_desc = sorted( list_of_tuples, key=lambda t: t[0], reverse=True ) # 👇️ [(3, 22), (2, 44), (1, 33)] print(sorted_list_desc)`

If the

`reverse`

argument is set to `True`

, then the elements are sorted as if each comparison were reversed.If you need to sort by another element, adjust the index accessor in the lambda function.

Here is an example that sorts the list of tuples by the third (index `2`

) item
in each tuple.

main.py

`list_of_tuples = [(2, 44, 100), (3, 22, 25), (1, 33, 50)] sorted_list = sorted( list_of_tuples, key=lambda t: t[2] ) # # 👇️ [(3, 22, 25), (1, 33, 50), (2, 44, 100)] print(sorted_list)`

Instead of using a lambda function, you can also use the `operator.itemgetter()`

method to specify the index you want to sort by.

main.py

`from operator import itemgetter list_of_tuples = [(2, 44), (3, 22), (1, 33)] sorted_list = sorted( list_of_tuples, key=itemgetter(0) ) # # 👇️ [(1, 33), (2, 44), (3, 22)] print(sorted_list)`

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

For example, `x = itemgetter(0)`

and then calling `x(my_tuple)`

, returns
`my_tuple[0]`

.

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 Nth element in place.

main.py

`from operator import itemgetter list_of_tuples = [(2, 44), (3, 22), (1, 33)] list_of_tuples.sort(key=itemgetter(0)) # # 👇️ [(1, 33), (2, 44), (3, 22)] 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:

Name | Description |
---|---|

key | a function that takes 1 argument and is used to extract a comparison key from each list element |

reverse | a 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.