How to sort a list of tuples by the second value in Python

This tutorial tells us how to Sort a list of tuples by the Second Value in Python. You can understand sorting by the following example. If you want to learn more about Python Programming, Visit Python Programming Tutorials.

Input : [('tech', 21), ('en', 7), ('in', 40)] 
Output : [('en', 7), ('tech', 21), ('in', 40)]

Input : [('1', 10), ('3', 5), ('4', 20), ('9', 15)]
Output : [('3', 5), ('1', 10), ('9', 15), ('4', 20)]

You can sort a list of tuples either in ascending or descending order. Similarly, you can sort the list of tuples by the first element, second element, or the last element.

Sort the list of tuples by the first element using sort() function

This function takes a list of tuples that are filled with random numbers. Sort() command sorts out the list of the tuples in ascending order by the first element.

Ascending order

#Using Sort() Function

list_Of_Tuples= [(2,8),(1,5),(0,9)]

print(f"Original List of tuples: {list_Of_Tuples}")

list_Of_Tuples.sort()

print(f"List of tuples after sorting: {list_Of_Tuples}")

#Code ends here
OUTPUT:
Original List of tuples: [(2, 8), (1, 5), (0, 9)]
List of tuples after sorting: [(0, 9), (1, 5), (2, 8)]

Descending order

#Using sort() Function for descending order

list_Of_Tuples= [(2,8),(1,5),(0,9)]

list_Of_Tuples.sort(key = lambda x: x[0], reverse=True)

print(list_Of_Tuples)
OUTPUT:
Original List of tuples: [(2, 8), (1, 5), (0, 9)]
List of tuples after sorting: [(2, 8), (1, 5), (0, 9)]

Sort the list of tuples by the second element

In this method, we are using the operator statement with the sort() function. You just have to give the index value to the itemgetter(). For example, here we have passed value 1 to the operator.

#Sorting by second element

import operator

list_Of_Tuples= [(2,8),(1,5),(0,9)]

print(f"Original List of tuples: {list_Of_Tuples}")

list_Of_Tuples.sort(key=operator.itemgetter(1))

print(f"List of tuples after sorting: {list_Of_Tuples}")

#Code ends here
OUTPUT:
Original List of tuples: [(2, 8), (1, 5), (0, 9)]
List of tuples after sorting: [(1, 5), (2, 8), (0, 9)]

using lambda function to sort a list

In this method, we have used the lambda function to sort out the tuples of the list. Here we will give the index in the item to specify which index has to be taken into notice while sorting the whole list of tuples.

#Using Lambda Function

list_Of_Tuples = [('Faizan', 1999),('Ali', 1994),('Shahroz', 1990)]

print(f"Original List of tuples: {list_Of_Tuples}")

list_Of_Tuples.sort(key= lambda item:item[1])

print(f"List of tuples after sorting: {list_Of_Tuples}")

#Code ends here
OUTPUT:
Original List of tuples: [('Faizan', 1999), ('Ali', 1994), ('Shahroz', 1990)]
List of tuples after sorting: [('Shahroz', 1990), ('Ali', 1994), ('Faizan', 1999)]
Here we can see that smallest number 1990 came first then 1994 and at last 1999. 

Using the sorted() function to Sort the list of tuples By first or second value

In this method, we will sort the list of tuples with the help of the sorted() function by passing the list into this function instead of making the object.

# Using Sorted() Function

price = [('Mutton', '540.3'),('Beaf', '190.3'),('Onion', '20.3')]

print(f"Original List of tuples: {price}")

list_Of_Tuples = sorted(price, key = lambda  x: float(x[1]))

print(f"List of tuples after sorting: {list_Of_Tuples}")

# Code ends here
OUTPUT:

Original List of tuples: [('Mutton', '540.3'), ('Beaf', '190.3'), ('Onion', '20.3')]

List of tuples after sorting: [('Onion', '20.3'), ('Beaf', '190.3'), ('Mutton', '540.3')]

Using the bubble sort algorithm

The index variable specifies the position by which sorting has to be carried out, which in this case, is the second element.

#Code starts here

list = [('John' , 86), ('Joe', 91) , ('Harry', 88) , ('Sam', 84), ('leo', 89)]

index = 1

for i in range(0, len(list)):

    for j in range(0, len(list)-i-1):  

        if (list_[j][index] > list_[j + 1][index]):  

            temp = list_[j]  

            list_[j]= list_[j + 1]  

            list_[j + 1]= temp  

print(list)

#Code ends here
Output: 

[('Sam',84), ('John',86), ('Harry',88), ('leo',89), ('Joe',91)]

If you want to know more about Python Programming, see our other tutorials. If you have any feedback, let us know. See more Python Tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *