Python program to sort a list of tuples by second item

Here, we have a list of tuples and we need to sort a list of tuples by second item in Python programming language.
By Shivang Yadav Last updated : December 12, 2023

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Sorting a list of tuples by second item

In this program, we have a list of tuples and we need to sort the list of tuples by the index of sorting which will be the second item of the tuple.

We basically will use a sorting algorithm but instead of using the Ist value of the list, we will use the second element of the tuple.

Example

Input:
[(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]

Output:
[(9, 1), (2, 5), (4, 6), (1, 7), (2, 8)]

In Python programming language, there are multiple ways to perform a single task in different ways and it totally depends on the programmer and then the need of the software being developed that which one should be used.

Method 1: Using binary sorting technique

Using binary sorting technique for sorting. We will access the second element of the tuple as an index for sorting the list.

Program

# Python program to sort a list of tuples by second item

# Creating a new tuple 
tupleList = [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
print("Unordered list : ", str(tupleList))

# Sorting the list of tuples using second item 
listLen = len(tupleList)
for i in range(0, listLen):
    for j in range(0, (listLen - i - 1)):
        if(tupleList[j][1] > tupleList[j+1][1]):
            temp = tupleList[j]
            tupleList[j] = tupleList[j+1]
            tupleList[j+1] = temp

# Printing sorted list 		
print("Sorted List : ", str(tupleList))

Output:

Unordered list :  [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
Sorted List :  [(9, 1), (2, 5), (4, 6), (1, 7), (2, 8)]

Method 2: Using sorting methods

Python provides us with some built-in sorting methods. While using the sorting methods, we need to pass a method to the method which will swap the element to the second element of tuple.

Program 1: Using sort() method

# Python program to sort a list of tuples by second item

# Creating a new tuple 
tupleList = [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
print("Unordered list : ", str(tupleList))

# Sorting the list of tuples using second item 
tupleList.sort(key = lambda val: val[1]) 

# Printing sorted list 		
print("Sorted List : ", str(tupleList))

Output:

Unordered list :  [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
Sorted List :  [(9, 1), (2, 5), (4, 6), (1, 7), (2, 8)]

Program 2: Using sorted() method

# Python program to sort a list of tuples by second item

# Creating a new tuple 
tupleList = [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
print("Unordered list : ", str(tupleList))

# Sorting the list of tuples using second item 
sortedTuple  = sorted(tupleList, key = lambda val: val[1])

# Printing sorted list 		
print("Sorted List : ", str(sortedTuple))

Output:

Unordered list :  [(2, 5), (9, 1), (4, 6), (2, 8), (1, 7)]
Sorted List :  [(9, 1), (2, 5), (4, 6), (1, 7), (2, 8)]

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.