In Python, we can sort a list of tuples by the second element of each tuple easily. The easiest way to sort a list of tuples by the second element is using the sort() function and a lambda expression.
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 sorted to sort a list of tuples by the second element in Python.
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)]
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, by default, the list is sorted by looking at the first element of the tuple.
To sort by the second element of each tuple, 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)]
How to Sort List of Tuples by Second Element 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 by second element in descending order with sort() with Python.
list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]
list_of_tuples.sort(key=lambda t: t[1], reverse=True)
print(list_of_tuples)
#Output:
[('pear', 5), ('lime', 4), ('apple', 3), ('banana', 1)]
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, key=lambda t: t[1], reverse=True)
print(sorted_list)
#Output:
[('pear', 5), ('lime', 4), ('apple', 3), ('banana', 1)]
Hopefully this article has been useful for you to learn how to sort a list of tuples in Python by the second element of each tuple.