Python Pool: HOW TO PYTHON SORT LIST OF TUPLES

Hello geeks and welcome in today’s article, we will cover a Python sort list of tuples with various examples. Along with the various examples, we will also look at different methods by which we can achieve it. We will primarily focus on the inbuilt function and sorting algorithm to achieve sorting of tuples. The ordered nature and unchangeable trait of tuple make it different from that of a common list.

Contents of Tutorial

DIFFERENT METHOD FOR PYTHON SORT LIST OF TUPLE

A tuples list can be sorted like any ordinary list. But as a tuple can contain multiple elements, which leaves us with the freedom to sort tuple based on its 1st term or ith term. The primary methods that we will look at in detail are:

  • Inbuilt list.sort()
  • Bubble sort()
  • Sorted()

1. INBUILT LIST.SORT() TO PYTHON SORT LIST OF TUPLE

SYNTAX

The syntax for tuple sort using inbuilt list.sort() is:

list_name.sort( key = lambda x:x[i])  

above here the i refers to the ith element of the tuple.

EXAMPLE

#sort using inbuilt list.Sort()
#tuple having structure(name, age)
list_siblings=[("Rohit",19),("Rishabh",14),("Ram",23),
("Navya",17),("Aditi",22)]
list_siblings(key= lambda x:x[1])
print(list_siblings)

OUTPUT:

[("Rishabh",14),("Navya",17),("Rohit",19),
("Aditi",22),("Ram",23)]

EXPLANATION

So in the above example, we used the inbuilt list.sort () method to sort the tuple using the
similar syntax as stated above, and we get perfectly sorted topple arranged in ascending
order based on the ages of siblings. We can also get the tuple sorted in descending order
by making a minute change in the above code.
list_siblings(key= lambda x:x[1], reverse=true)
This change would yield us a descending order series.

2. SORT LIST USING BUBBLE SORT

EXAMPLE

Now let us look at siblings’ problems and try to solve them using the bubble sort method. Here our motive is to sort the elements based on the 0th term of the tuple.

list_=[("Rohit",19),("Rishabh",14),("Ram",23),
("Navya",17),("Aditi",22)]
 ith=0
 list-lenght=len(list_) 
 for i in range(0,list_lenght)
 for j in range(0,list_lenght-i-1);
 if(list_[j]>list_[i]);
 temp=list_[j]
 list_[j]=list_[j+1]
 list_[j+1]=temp
print(list_)

OUTPUT:

[("Aditi",22),("Navya",17),("Ram",23),
("Rishabh",14),("Rohit",19)]

EXPLANATION

In the above example, our motive was to sort the tuple based on the 0th element that is
sibling name using the bubble sort method. We can observe that we have to write much more
of a code than the inbuilt method. It is also not considered the best method for a lot
of time and memory. But on the positive part, it’s simplest of all and easy to
construct and understand.

3. SORTED() METHOD TO PYTHON SORT LIST OF TUPLE

The sorted function sorts the given input in a specific order and returns sorted iterable as a list. now let us look at an example related to this method which will make things much clearer for us.

EXAMPLE

#example1
late = ('19', '17', '14', '10', '11')
print(sorted(late))

#example2
list=[("Rohit",19),("Rishabh",14),("Ram",23),
("Navya",17),("Aditi",22)]
print(sorted(list))

OUTPUT:

#output1
[10,11,14,17,19]
#output2
[('Aditi', 22), ('Navya', 17), ('Ram', 23), ('Rishabh', 14), ('Rohit', 19)]

Explanation:

Here we have 2 different examples where we have used the sorted() method. In the first example, we have taken a variable and added a couple of numbers to it. Here we get an array that is arranged in ascending order. Whereas for our second example, we have considered the data as for the above methods. Here also, we get an array arranged in ascending order as per the names of different siblings’ names.

CONCLUSION

In this article, we covered the python list sort of tuples. We looked at various methods, syntax, and examples. We also understood that a couple could be sorted similarly to that of an ordinary list. I hope this article was able to clarify your doubts. If anyhow still any doubt remains, then feel free to comment it down below. Done reading this, why not read NumPy any next.

Comments are closed.