Python – Sort list of tuples by second element

Python – Sort list of tuples by second element

To sort a list of tuples by second element in each tuple in Python, call sorted() built-in function, pass the given list of tuples as first argument, and the key function that returns the second item in the tuple.

The second way or sorting a list of tuples by second element is using list sort() method. Call sort() method on the list of tuples, and pass a lambda function for key named argument that gets the second element of each tuple.

In this tutorial, you will learn how to sort list of tuples by second element in each tuple, using sorted() built-in function or list sort() method.

1. Sort list of tuples by second element using sorted() built-in function

In this example, we take a list of tuples in list_of_tuples variable with some initial values. Each tuple is pair of two elements where the first element is a string and the second element is an integer value.

We have to sort this list based on the second element of the inner tuples using sorted() built-in function.

  1. Given a list of tuples object in list_of_tuples.
  2. Call sorted() built-in function, and pass the list_of_tuples as first argument, and lambda function lambda x: x[1] that returns the second element in the tuple as second argument.
  3. sorted() function returns the sorted list. Assign it to a variable, say sorted_list.
  4. You may print the sorted list to output.

Python Program

list_of_tuples = [
	('apple', 25),
	('banana', 54),
	('cherry', 10),
	('fig', 36)
]

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

print(sorted_list)
Run Code Copy

Output

[('cherry', 10), ('apple', 25), ('fig', 36), ('banana', 54)]

The tuples have been ordered in the ascending order of the second item in the tuples.

If you would like to order the items in descending order, you may reverse the sorted list sorted_list using reversed() built-in function, as shown in the following program.

Python Program

list_of_tuples = [
	('apple', 25),
	('banana', 54),
	('cherry', 10),
	('fig', 36)
]

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

print(list(sorted_list))
Run Code Copy

Output

[('banana', 54), ('fig', 36), ('apple', 25), ('cherry', 10)]

2. Sort list of tuples by second element using List sort() method

We shall consider the same list of tuples that we used in the previous example. We have to sort this list based on the second element of the inner tuples using list sort() method.

  1. Given a list of tuples object in list_of_tuples.
  2. Call sort() method on the list_of_tuples and pass lambda function lambda x: x[1] for the key named argument.
  3. sort() function sorts the list in-place.
  4. You may print the sorted list, list_of_tuples, to output.

Python Program

list_of_tuples = [
	('apple', 25),
	('banana', 54),
	('cherry', 10),
	('fig', 36)
]

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

print(list_of_tuples)
Run Code Copy

Output

[('cherry', 10), ('apple', 25), ('fig', 36), ('banana', 54)]

List sort() method have sorted the list of tuples based on the second element in the tuples.

Summary

In this tutorial, we learned how to sort a given list of tuples by second element in the tuples using sorted() built-in function, or list sort() method, with well detailed examples for each of the approaches.

Related Tutorials

Code copied to clipboard successfully 👍