How To Sort a List of Tuples in Python

This article will discuss how to sort a list of tuples. The list is a data structure in Python used to store elements with multiple data types. We can create a list by using [].

A tuple is a data structure in python used to store elements with multiple data types. We can create a tuple by using ().

Also ReadArithmetic Operators in Python [With Examples]

How to sort a list of tuples in Python

A collection of tuples in a list is known as a list of tuples.

Structure:

[(tuple1),(tuple2),.............,(tuplen)]

Now, let’s see various methods to sort a list of tuples in Python.

Also ReadHow to Create Python Empty Set

Method 1: Using sort()

We can sort the list of tuples by using the sort() method.

Syntax:

my_list_of_tuples.sort(reverse)

Reverse accepts two parameters. If it is true, then it will sort the list of tuples in descending order; if it is set to False, it will sort the list of tuples in ascending order.

Example 1:

In this example, we will create a list of tuples with 5 elements and sort them in descending order.

Code:

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in descending order
my_list_of_tuple.sort(reverse=True)


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(5, 'html'), (4, 'python'), (3, 'node-js'), (2, 'mysql'), (1, 'php')]

Also ReadPython Matrix Using Numpy [With Examples]

Example 2:

In this example, we will create a list of tuples with 5 elements and sort them in ascending order

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in ascending  order
my_list_of_tuple.sort(reverse=False)


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(1, 'php'), (2, 'mysql'), (3, 'node-js'), (4, 'python'), (5, 'html')]

Also ReadHow To Create An Empty Dictionary in Python

Example 3:

In this example, we will create a list of tuples with 5 elements and sort them in ascending order by the second element in the tuple.

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in ascending  order by second item in a tuple
my_list_of_tuple.sort(key = lambda x: x[1])


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(5, 'html'), (2, 'mysql'), (3, 'node-js'), (1, 'php'), (4, 'python')]

Example 4:

In this example, we will create a list of tuples with 5 elements and sort them in descending order by the second element in the tuple.

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in ascending  order by second item in a tuple
my_list_of_tuple.sort(key = lambda x: x[1],reverse=True)


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(4, 'python'), (1, 'php'), (3, 'node-js'), (2, 'mysql'), (5, 'html')]

Also ReadHow to Exit Python Program [4 Methods]

Example 5:

In this example, we will create a list of tuples with 5 elements and sort in descending order by the first element in the tuple.

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in ascending  order by first item in a tuple
my_list_of_tuple.sort(key = lambda x: x[0],reverse=True)


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(5, 'html'), (4, 'python'), (3, 'node-js'), (2, 'mysql'), (1, 'php')]

Also ReadHow to Convert Python Tuples to Lists

Example 6:

In this example, we will create a list of tuples with 5 elements and sort them in ascending order by the first element in the tuple.

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#sort in ascending  order by first item in a tuple
my_list_of_tuple.sort(key = lambda x: x[0])


#display
print(my_list_of_tuple)

Output:

[(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(1, 'php'), (2, 'mysql'), (3, 'node-js'), (4, 'python'), (5, 'html')]

Also ReadHow to Handle String Index Out of Range Error In Python

Method 2 : Using reverse()

We can sort the list of tuples in reverse order by using the reverse() method.

Syntax:

my_list_of_tuples.reverse()

Example :

In this example, we will create a list of tuples with 5 elements and sort them in reverse order

#create list of tuples with 5 tuples in a list
#each tuple contains two elements
my_list_of_tuple=[(1,"php"),(4,"python"),(5,"html"),(3,"node-js"),(2,"mysql")]

#display
print(my_list_of_tuple)

#reverse in decreasing order
my_list_of_tuple.reverse()


#display
print(my_list_of_tuple)

Output:

(1, 'php'), (4, 'python'), (5, 'html'), (3, 'node-js'), (2, 'mysql')]
[(2, 'mysql'), (3, 'node-js'), (5, 'html'), (4, 'python'), (1, 'php')]

Also ReadPython For Loop Index With Examples

Summary

In this tutorial, we discussed how to sort the list of tuples by elements in a tuple using sort(), and we reversed the elements in a list of tuples by using the reverse() method.

Other articles you might like:

Scroll to Top