# Python Sort List of Tuples

## 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, we will learn how to sort a list of tuples based on the first, second, or ith element in the tuples. 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.

### Example 1: Sort List of Tuples using list.sort()

In this example, we take tuples containing name and marks of students in a class. We shall sort these tuples based on the marks, the second element in all the tuples.

Python Program

``````list_students = [('Saranya',84), ('Surya',92) , ('Joy',88) , ('Sree',86), ('Ritha',89)]

#sort by second element of tuple
list_students.sort(key = lambda x: x)   #index 1 means second element

print(list_students)``````
Run

Output

``[('Saranya', 84), ('Sree', 86), ('Joy', 88), ('Ritha', 89), ('Surya', 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 also 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

``````list_students = [('Saranya',84), ('Surya',92) , ('Joy',88) , ('Sree',86), ('Ritha',89)]

#sort by second element of tuple
list_students.sort(key = lambda x: x, reverse=True)

print(list_students)``````
Run

Output

``[('Surya', 92), ('Ritha', 89), ('Joy', 88), ('Sree', 86), ('Saranya', 84)]``

### Example 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

``````list_ = [('Saranya',84), ('Surya',92) , ('Joy',88) , ('Sree',86), ('Ritha',89)]

#sort by second element of tuple
ith = 1
list_length = len(list_)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (list_[j][ith] > list_[j + 1][ith]):
temp = list_[j]
list_[j]= list_[j + 1]
list_[j + 1]= temp

print(list_)``````
Run

Output

``[('Saranya', 84), ('Sree', 86), ('Joy', 88), ('Ritha', 89), ('Surya', 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

``````list_ = [('Saranya',84), ('Surya',92) , ('Joy',88) , ('Sree',86), ('Ritha',89)]

#sort by second element of tuple
ith = 0
list_length = len(list_)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (list_[j][ith] > list_[j + 1][ith]):
temp = list_[j]
list_[j]= list_[j + 1]
list_[j + 1]= temp

print(list_)``````
Run

Output

``[('Joy', 88), ('Ritha', 89), ('Saranya', 84), ('Sree', 86), ('Surya', 92)]``

### Summary

In this tutorial of Python Examples, we learned how to sort a list of tuples using inbuilt functions and sorting algorithms, with the help of well detailed examples.