Last Updated on July 7, 2021 by Roshan Parihar
In this tutorial, learn how to sort list of tuples in ascending order or increasing order using Python. The short answer is to use the sort()
with 'key=lambda'
for sorting tuples in a list.
You can also sort the list of tuples using the sorted()
and 'key=itemgetter()'
. If you have a list of tuples and each tuple contains two elements, you can sort them according to the 1st element of and 2nd element of each tuple. Let’s find out the different methods with the examples given below here.
Method 1: Using sort() and key=lambda to Sort List of Tuples in Python
Sort by 1st Element
Firstly, you have to use the sort()
. Secondly, inside that function, use the 'key=lambda'
and pass zero(0) as the argument of tup[0]
. For example, look at the use of the function given below.
1 2 3 4 5 6 7 8 |
#Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sort list of tuples using sort() and key=lambda myListofTuple.sort(key=lambda tup: tup[0]) #Print result print(myListofTuple); |
Output
As a result, the above example shows the output that contains the sorted list of tuples according to the 1st element of each tuple.
Sort by 2nd Element
Similarly, to perform this task according to the 2nd element of each tuple, you have to use the same example given above and pass one (1) as the argument of tup[1]
.
1 2 3 4 5 6 7 8 |
#Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sort list of tuples using sort() and key=lambda myListofTuple.sort(key=lambda tup: tup[1]) #Print result print(myListofTuple); |
Output
The above example shows the output that contains the sorted list of tuples according to the 2nd element of each tuple.
Method 2: Using sorted() and key=lambda to Sort List of Tuples Using Python
Sorting by 1st Element
When you want to sort the list of tuples in Python using sorted()
, you have passed the list of tuples variable with 'key=lambda'
as its argument. Also, for sorting them according to the 1st element of each tuple, you need to pass zero (0) as the tup[0]
argument.
1 2 3 4 5 6 7 8 |
#Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sorting list of tuples using sorted() and key=lambda mysortedListofTuple = sorted(myListofTuple, key=lambda tup: tup[0]) #Print result print(mysortedListofTuple); |
Output
The output shows the list of tuples that are sorted according to the 1st element of each tuple.
Sorting by 2nd Element
Similarly, for sorting the list of tuples according to the 2nd element of each tuple, you have to use the above same method and pass one (1) as the argument of tup[1]
.
1 2 3 4 5 6 7 8 |
#Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sorting list of tuples using sorted() and key=lambda mysortedListofTuple = sorted(myListofTuple, key=lambda tup: tup[1]) #Print result print(mysortedListofTuple); |
Output
Method 3: Using sort() and key=itemgetter() in Python
In addition to the above methods, you can also sort the list of tuples in Python using the sort()
with 'key=itemgetter()'
. Let’s learn the method with the examples given below.
Sort by 1st Element
If you want to sort the list of tuples, you can also use the sort()
with 'key=itemgetter()
as its argument.
Most importantly, for sorting the elements according to the 1st element of each tuple in a list, you have to pass zero(0) as the argument of itemgetter(0)
.
1 2 3 4 5 6 7 8 9 10 11 |
#Import Python library from operator import itemgetter #Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sort list of tuples using sort() and key=itemgetter() myListofTuple.sort(key=itemgetter(0)) #Print result print(myListofTuple); |
Output
You can check the above output with a list of tuples that are sorted according to the 1st element of each tuple.
Sort by 2nd Element
Likewise, you can sort the list of tuples according to the 2nd element by passing one (1) as the argument of itemgetter()
in Python.
1 2 3 4 5 6 7 8 9 10 11 |
#Import Python library from operator import itemgetter #Define list of tuples in Python myListofTuple = [(1,2), (3,5), (6,3), (2, 3)]; #Sort list of tuples using sort() and key=itemgetter() myListofTuple.sort(key=itemgetter(1)) #Print result print(myListofTuple); |
Output
As a result, it gives the same result of sorted elements according to the 2nd items of each tuple in a list.