Python sort list of tuples

By: On:

In this Python tutorial, we will learn about the Python sort list of tuples. Also, we will discuss:

  • Python sort list of tuples
  • Python sort list of tuples by second element
  • How to sort list of tuples by first element descending in Python
  • How to sort list of tuples in Python by length

How to sort list of tuples in Python? In Python, we can use the sort() method to sort the elements in the ascending order and by default, it will sort the first element.

Python sort list of tuples

The sort() method is used to sort the elements in the ascending order and by default, it will sort the first element. If two tuples have the same first element then it will sort them according to the second element.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort()
print(roll_list1)

After writing the above code (python sort lists of tuples), Ones you will print “roll_list1” then the output will appear as a “ [(‘Beneth’, 78), (‘Cirus’, 77), (‘Faiz’, 79), (‘Jack’, 76)] ”. Here, sort() method is used to sort the elements in ascending order and it will be sorted by its first element. You can refer to the below screenshot for creating python sort lists of tuples.

Python sort list of tuples
Python sort list of tuples

Python sort list of tuples by second element

In python, if you want to sort a list of tuples by the second element then we have the function called sort() and by using lambda function as a key function. A custom comparator is a key function in sort().


Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort(key=lambda a: a[1])
print(roll_list1)

After writing the above code (python sort list of tuples by the second element), Ones you will print “roll_list1” then the output will appear as a “ [(‘Jack’, 76), (‘Cirus’, 77), (‘Beneth’, 78), (‘Faiz’, 79)] ”.

Here, sort() method is used to sort the elements in ascending order and the list of tuples will be sorted by its second element.

Check the screen shot.

Python sort list of tuples by second element
Python sort list of tuples by second element

Python sort list of tuples by first element descending

In python, to sort list of tuples by the first element in descending order, we have to use the sort() method with the parameter ” (reverse=True) “ which will sort the elements in descending order.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort(reverse=True)
print(roll_list1)

After writing the above code (python sort list of tuples by first element descending), Ones you will print “roll_list1” then the output will appear as a “ [ (‘Jack’, 76), (‘Faiz’, 79), (‘Cirus’, 77), (‘Beneth’, 78)] ”.

Here, sort() method is used with the parameter as “(reverse= True)” to sort the elements in descending order and it will be sorted by its first element.

Check the screen shot.

Python sort list of tuples by first element descending
Python sort list of tuples by first element descending

Python sort list of tuples by length

In python, to sort list of tuples by length, we have the sort() method and the parameter should be pass to get the length of the first element.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('sid', 79)]
roll_list1.sort(key=lambda a: len(a[0]))
print(roll_list1)

After writing the above code (python sort list of tuples by length), Ones you will print “roll_list1” then the output will appear as a “ [(‘Sid’, 79), (‘Jack’, 76), (‘Cirus’, 77), (‘Beneth’, 78)] ”. Here, sort() method is used with the parameter, and the length is calculated and the element is sorted in ascending order by its first element. You can refer to the below screenshot for the python sort list of tuples by length.

Python sort list of tuples by length
Python sort list of tuples by length

You may like the following Python tutorials:

In this tutorial, we have discussed how to sort list of tuples in python. We discussed the Python sort list of tuples with examples.

Leave a Comment