Here are Python program to sort a list of tuples by second Item coding

Given a listing of tuples, write a Python program to sort the tuples by the second merchandise of every tuple.

Examples:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and study the fundamentals.

To start with, your interview preparations Enhance your Data Structures ideas with the Python DS Course. And to start together with your Machine Learning Journey, be part of the Machine Learning – Basic Level Course

Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)] 
Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]

Input : [('452', 10), ('256', 5), ('100', 20), ('135', 15)]
Output : [('256', 5), ('452', 10), ('135', 15), ('100', 20)]

Method #1: Using the Bubble Sort

 

Using the strategy of Bubble Sort to we will carry out the sorting. Note that every tuple is a component within the given listing. Access the second ingredient of every tuple using the nested loops. This performs the in-place technique of sorting. The time complexity is just like the Bubble Sort i.e. O(n^2).

 

def Sort_Tuple(tup): 

     

    

    lst = len(tup) 

    for i in range(0, lst): 

         

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

            if (tup[j][1] > tup[j + 1][1]): 

                temp = tup[j] 

                tup[j]= tup[j + 1

                tup[j + 1]= temp 

    return tup 

 

tup =[('for', 24), ('is', 10), ('Geeks', 28), 

      ('Geeksforgeeks', 5), ('portal', 20), ('a', 15)] 

       

print(Sort_Tuple(tup)) 

Output:

[('Geeksforgeeks', 5), ('is', 10), ('a', 15), ('portal', 20), ('for', 24), ('Geeks', 28)]

Method #2: Using sort() technique

While sorting by way of this technique the precise content material of the tuple is modified, and similar to the earlier technique, the in-place technique of the sort is carried out.

 

def Sort_Tuple(tup): 

 

    

    

    

    tup.sort(key = lambda x: x[1]) 

    return tup 

 

tup = [('rishav', 10), ('akash', 5), ('ram', 20), ('gaurav', 15)] 

 

print(Sort_Tuple(tup)) 

Output:

[('akash', 5), ('rishav', 10), ('gaurav', 15), ('ram', 20)]

Method #3: Using sorted() technique

Sorted() technique types a listing and all the time returns a listing with the weather in a sorted method, with out modifying the unique sequence. It takes three parameters from which two are optionally available, right here we tried to make use of all the three:

Iterable : sequence (listing, tuple, string) or assortment (dictionary, set, frozenset) or another iterator that must be sorted.
Key(optionally available) : A perform that will function a key or a foundation of sort comparability.
Reverse(optionally available) : To sort this in ascending order we may have simply ignored the third parameter, which we did on this program. If set true, then the iterable could be sorted in reverse (descending) order, by default it’s set as false.

 

def Sort_Tuple(tup): 

 

    

    

    

    return(sorted(tup, key = lambda x: x[1]))  

 

tup = [('rishav', 10), ('akash', 5), ('ram', 20), ('gaurav', 15)] 

 

print(Sort_Tuple(tup)) 

Output:

[('akash', 5), ('rishav', 10), ('gaurav', 15), ('ram', 20)]

 

Add a Comment

Your email address will not be published.