How to Sort a List of Tuples? – Most Pythonic Way!

Did you already try to sort a list of tuples? And when you saw the result, it wasn’t quite what you expected? Well, this happened to me when I went deeper into Python, and I suppose it also happened to you since you are looking for a solution. To me, this shows that you are an advanced Python programmer!

Quick Answer

Use Python’s built-in sorted() function or call the method sort() on the list you want to sort. Both of them have an optional parameter key that expects a function. Pass a function here, which computes a key-value from each tuple in the list to sort the tuples by their computed key-values. Example of sorting a list of tuples by their second entry:

lst = [(1, 2), (2, 3), (0, 3)]
lst.sort(key=lambda x: x[1])

Exercise: Add a print() statement and print the state of the original list lst.

How to Sort a List of Tuples by Any Element or Custom Value?

Let’s first frame our problem in more detail: We have a list of tuples that we want to sort by their second element or sum of elements or anything else that is not the first element of the list. The default behavior of both sort() and sorted() is to use the first entry of each tuple as a sorting criterion. This is what may lead to surprises for beginners.

Suppose, we want to sort a list of items like this one:

items = [
   ("apple", 1.5),
   ("pear", 2.3),
   ("egg", 0.5),
   ("cherry", 0.2),
]

We could either sort the tuples by their first element, which is the name, or by their second element, the item’s price. To achieve this, we could write a custom sort function. But this wouldn’t be very Pythonic.

Instead we want to use the built-in functions which Python provides. Therefore we have to options:

  1. call sort() on the list via items.sort()
  2. use sorted() to sort the list via sorted_items = sorted(items)

The difference between the two options is that the first one sorts the list in place and the second one creates a new list instance and adds the sorted items to this new instance. So in both cases, you end up with a sorted list. If you need to preserve the list’s initial state use sorted() in any other case you should prefer calling sort() directly on the list. In both cases, the list will be sorted by Python’s default: Each tuple’s first entry.

To override the default behavior we use the optional parameter key which is provided by both sort() and sorted(). The parameter key expects a function that can compute a value from a tuple. It is very common to use a lambda to override the default behavior. If you are not yet familiar with Python’s lambdas read all the background knowledge in this article.

To sort the list items by the tuples’ second entry we write:

items.sort(key=lambda x: x[1])


or if you want to preserve the initial order of the items:

new_list = sorted(items, key=lambda x: x[1])

Our key function takes a tuple x and returns its second entry, thus, the final ordering of the list will only take into account each tuple’s second entry. We could also write:

items.sort(key=lambda x: sum(x))


to sort the tuples by the sum of their entries. Of course, this is not applicable for our example, since the entries of our tuples are strings and integers.

Finally, it is also important to be aware of Python’s default behavior for sorting lists of tuples. It doesn’t make sense to write:

items.sort(key=lambda x: x[0])

because this is just what the default behavior does.

To sum it all up, watch this video:

Conclusion

Python provides everything you need to sort lists of tuples easily without writing custom sorting functions. All you need to do is define the key-function which computes a value for each tuple by which they should be sorted.