Q:

Python program to sort a list of tuples by second item

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

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.

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)]

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to sort a list of tuples in increas... >>
<< Python program to join tuples if similar initial e...