In Python, we can sort a list of tuples easily. The easiest way to sort a list of tuples is using the sort() function.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort()

print(list_of_tuples)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

You can also use sorted to sort a list of tuples in Python.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples)

print(sorted_list)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

In Python, tuples are a collection of objects which are ordered and mutable. When working with a list of tuples, it can be useful to be able to sort the tuples in a list.

To sort a list of tuples in Python, we can use the standard list sort() and sorted() functions.

The Python list sort() function takes a list and sorts it. The Python list sorted() function takes a list as an argument and returns the sorted list.

When sorting a list of tuples, the list is sorted by looking at the first element of the tuple.

Below is an example in Python of how to use sort() to sort a list of tuples.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort()

print(list_of_tuples)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

Depending on the situation, you may want to keep the original list and get a new sorted list. You can use sorted() to return the sorted list of tuples as shown in the following Python code.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples)

print(sorted_list)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

How to Sort List of Tuples Descending in Python

When using the sort() or sorted() function to sort a list, the default is that the items will be sorted in ascending order (smallest to biggest, first to last, etc.).

We can easily sort a list of tuples in descending order. To sort a list of tuples in descending order, you just need to pass “reverse=True” to either sort() or sorted().

Below is an example of sorting a list of tuples in descending order with sort() with Python.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort(reverse=True)

print(list_of_tuples)

#Output:
[('pear', 5), ('lime', 4), ('banana', 1), ('apple', 3)]

You can also use the sorted() function to sort a list of tuples in descending order.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples, reverse=True)

print(sorted_list)

#Output:
[('pear', 5), ('lime', 4), ('banana', 1), ('apple', 3)]

How to Sort List of Tuples by Second Element in Python

You are also easily able to sort a list of tuples by different elements in the tuple. For example, we can easily sort a list of tuples by the second element of each tuple.

To do so, you can pass a lambda expression to the ‘key’ argument of sort() or sorted().

Below is how to sort a list of tuples by the second element in Python using the sort() function.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

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

print(list_of_tuples)

#Output:
[('banana', 1), ('apple', 3), ('lime', 4), ('pear', 5)]

You can also use the sorted() function and pass a lambda expression to ‘key’ to sort by the second element of each tuple.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

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

print(sorted_list)

#Output:
[('banana', 1), ('apple', 3), ('lime', 4), ('pear', 5)]

Hopefully this article has been useful for you to learn how to sort a list of tuples in Python.

Categorized in:

Python,

Last Update: February 26, 2024