Skip to content

Sort List Of Tuples By Second value In Python

  • by
sort-list-of-tuples

Python is a highly versatile programming language and, like most languages, it allows you to use arrays.

The sorted list of tuples is returned with a value for a second. The Python programming language allows you to create ordered sequences called lists.

That can create in several different ways. One way, which we’ll look at here, involves sorting each element before adding it to the end.

This produces orderliness on your data without having any need whatsoever to sort out individual items manually!

In python, these arrays are known as lists, tuples, or dictionaries. Lists and dictionaries are straightforward things sort list of tuples by second value in python.

Tuple, for those who don’t know, uses to store multiple items in a single variable.

Contents

Sort List Of Tuples By Second value In Python

A tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage sort list of tuples by the second value.

A tuple is a collection that has ordered and is unchangeable. This article will show you how you can sort a list of tuples so without further ado, let us get straight into it.

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

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

Output : [(‘256’, 5), (‘452’, 10), (‘135’, 15), (‘100’, 20)]

Problem: You asked to write a code that will sort the tuples by the second item of each tuple. How are you to do that? Given below are some methods you can use.

Bubble Sort

We can see that each tuple is an element of the given list. So, to access the second element of each tuple, we can use nested for-loops.

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), (wholeblogs, 28), 
      (wholeblogs.com, 5), ('portal', 20), ('a', 15)]        
print(Sort_Tuple(tup)) 

Output:

[(‘wholeblogs.com’, 5), (‘is’, 10), (‘a’, 15), (‘portal’, 20), (‘for’, 24), (‘wholeblogs’, 28)]

Using the sort() method

The sort() method sorts the elements of a given list in a specific ascending or descending order.

def Sort_Tuple(tup): 
   tup.sort(key = lambda x: x[1]) 
    return tup 
  tup = [('ALI', 10), ('NADEEM', 5), ('UMER', 20), ('USMAN', 15)] 
  print(Sort_Tuple(tup)) 

Output:

[(‘NADEEM’, 5), (‘ALI’, 10), (‘USMAN), (‘UMER’, 20)]

CONCLUSION

To sort a list of tuples, one can use several methods. Mentioned above are the best and most efficient methods that will help you sort your list using the 2nd item of each tuple.

READ MORE:

Merge Multiple lists Of Dictionaries With the Same Value In PYTHON.

Python Program To Convert Hex String To Decimal.

Python merges dictionaries with the same value.

Leave a Reply

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