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.

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


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(key= lambda x:x[1])




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


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)]
for i in range(0,list_lenght):
    for j in range(0,list_lenght-i-1):


[('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.


late = ('19', '17', '14', '10', '11')

print(sorted(l, key= lambda x:x[1]))


[('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.

See Also


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.

Notify of
Inline Feedbacks
View all comments