Python program to sort a list of tuples in increasing order by the last element in each tuple

In this program, we have a list of tuples and we need to sort the list of tuples in increasing order by the last element in each tuple in Python programming language.
By Shivang Yadav Last updated : September 15, 2023

Overview

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)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].

Example:

list = [3 ,1,  5, 7]

A list of tuples is a list whose each element is a tuple.

Example:

upList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

Problem statement

Given a list of tuples, we need to sort the list of tuples in increasing order by the last element in each tuple.

Sorting a list of tuples in increasing order by the last element in each tuple

We need to sort the List of Tuples in Increasing Order By the last Element in Each Tuple. For this, we will be simply sorting the List but the value to be used while comparison will be the second value of the list.

We can write our sorting code or we can use built-in python methods to perform the task.

Let's see these ways:

Method 1: Writing sorting code

We can use any sorting technique and use the sorting value to be the second element of the tuple. Here, we will use bubble sort with tup[2] as the comparison value.

Program

# Python program to sort a list of tuples in 
# increasing order by the last element in each tuple

# Creating and Print list 
tupList =[(5, 7), (12, 4), (20, 13), (45, 2)]
print("List of Tuple before sorting : " + str(tupList))

# Sorting List of Tuples in Increasing order by last element 
listLen = len(tupList); 
for i in range(0, listLen):
    for j in range(0, listLen - i - 1):
        if(tupList[j][-1] > tupList[j + 1][-1]):
            swap = tupList[j]
            tupList[j] = tupList[j + 1]
            tupList[j + 1] = swap

#Printing Sorted List 
print("List of Tuple after sorting : " + str(tupList))

Output:

List of Tuple before sorting : [(5, 7), (12, 4), (20, 13), (45, 2)]
List of Tuple after sorting : [(45, 2), (12, 4), (5, 7), (20, 13)]

Method 2: Using sorted() and sort() methods

We can also perform the task of sorting by the last element using built-in methods provided in the python library.

There are two such methods to perform the task. They are,

(i) Sorted() method

The sorted method in the python library is used to return a sorted list. We will pass parameters to use the last value of tuple to sort the list based on the last value of the tuple.

Program

# Python program to sort a list of tuples in 
# increasing order by the last element in each tuple

def last(n):
    return n[-1]  

# Creating and Print list 
tupList =[(5, 7), (12, 4), (20, 13), (45, 2)]
print("List of Tuple before sorting : " + str(tupList))

# Sorting List of Tuples in Increasing order by last element 
sortedTupList = sorted(tupList, key=last)

#Printing Sorted List 
print("List of Tuple after sorting : " + str(sortedTupList))

Output:

List of Tuple before sorting : [(5, 7), (12, 4), (20, 13), (45, 2)]
List of Tuple after sorting : [(45, 2), (12, 4), (5, 7), (20, 13)]

(ii) sort() method

The sort() method also sorts the given list and takes a lambda function for defining the key and order of sorting.

Program

# Python program to sort a list of tuples in 
# increasing order by the last element in each tuple

# Creating and Print list 
tupList =[(5, 7), (12, 4), (20, 13), (45, 2)]
print("List of Tuple before sorting : " + str(tupList))

# Sorting List of Tuples in Increasing order by last element 
tupList.sort(key = lambda x: x[-1])

#Printing Sorted List 
print("List of Tuple after sorting : " + str(tupList))

Output:

List of Tuple before sorting : [(5, 7), (12, 4), (20, 13), (45, 2)]
List of Tuple after sorting : [(45, 2), (12, 4), (5, 7), (20, 13)]

Python Tuple Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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