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.
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()
1. Inbuilt list.sort() To Python Sort List Of Tuple
The syntax for tuple sort using the inbuilt list.sort() is:
list_name.sort( key = lambda x:x[i])
above here i refers to the ith element of the tuple.
#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) print(list_siblings)
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, reverse=true)
This change would yield us a descending order series.
2. Sort List Using Bubble Sort
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_)
[('Rishabh', 14), ('Navya', 17), ('Rohit', 19), ('Aditi', 22), ('Ram', 23)]
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 the 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.
#example1 late = ('19', '17', '14', '10', '11') print(sorted(late)) #example2 l=[("Rohit",19),("Rishabh",14),("Ram",23),("Navya",17),("Aditi",22)] print(sorted(l, key= lambda x:x))
#output1 [10,11,14,17,19] #output2 [('Rishabh', 14), ('Navya', 17), ('Rohit', 19), ('Aditi', 22), ('Ram', 23)]
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 for the above methods. Here also, we get an array arranged in ascending order as per the names of different siblings names.
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.