Contents
Sort list of tuples in Python
Consider that you have a list of tuples. You can sort the tuples in the list like you sort a list of integers.
Based on the fact that a tuple can contain multiple elements in it, you can sort this list of tuples based on some ith element of all the tuples.
In this tutorial, you will learn how to sort a list of tuple. There are many ways to do so. We shall look into examples for the following ways.
- Use inbuilt list.sort() method.
- Use bubble sort()
You can use any sorting() algorithm. The process should be same as that of using bubble sort given in this tutorial.
Examples
1. Sort list of tuples using list.sort()
In this example, we take a list of tuples where each tuple contains the fruit name and the number of fruits we have. We shall sort these tuples based on the second element in all the tuples, i.e., the number of fruits we have of each type.
Python Program
mylist = [('apple',84), ('banana',92) , ('cherry',88) , ('mango',86), ('lichi',89)]
# Sort by second element of tuples
mylist.sort(key = lambda x: x[1]) #index 1 means second element
print(mylist)
Run Code CopyOutput
[('apple', 84), ('mango', 86), ('cherry', 88), ('lichi', 89), ('banana', 92)]
The list of tuples is ordered in the increasing order of the second element in the tuples. You may change the order from increasing to decreasing by passing True for reverse parameter of sort() method. Or you can use list.reverse() on the ascending ordered list.
In the following example, we will sort the list of tuple in the descending order, using reverse parameter.
Python Program
mylist = [('apple',84), ('banana',92) , ('cherry',88) , ('mango',86), ('lichi',89)]
# Sort by second element of tuple in descending order
mylist.sort(key = lambda x: x[1], reverse=True)
print(mylist)
Run Code CopyOutput
[('banana', 92), ('lichi', 89), ('cherry', 88), ('mango', 86), ('apple', 84)]
2. Sort list of tuples using bubble sort algorithm
We shall take the same list of tuples in the above example and sort them based on the marks as before, but using bubble sort algorithm.
Python Program
mylist = [('apple',84), ('banana',92) , ('cherry',88) , ('mango',86), ('lichi',89)]
# Sort by second element of tuple
ith = 1
list_length = len(mylist)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (mylist[j][ith] > mylist[j + 1][ith]):
mylist[j], mylist[j + 1] = mylist[j + 1], mylist[j]
print(mylist)
Run Code CopyOutput
[('apple', 84), ('mango', 86), ('cherry', 88), ('lichi', 89), ('banana', 92)]
In the above program, ith variable defines the position in tuples, by which sorting has to be done. Let us now sort the list of tuples by the first element in the tuples.
Python Program
mylist = [('apple',84), ('banana',92) , ('cherry',88) , ('mango',86), ('lichi',89)]
# Sort by first element of tuple
ith = 0
list_length = len(mylist)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (mylist[j][ith] > mylist[j + 1][ith]):
mylist[j], mylist[j + 1] = mylist[j + 1], mylist[j]
print(mylist)
Run Code CopyOutput
[('apple', 84), ('banana', 92), ('cherry', 88), ('lichi', 89), ('mango', 86)]
Summary
In this tutorial of Python Examples, we learned how to sort a list of tuples using built-in functions and sorting algorithms, with the help of well detailed examples.