Last Updated on May 4, 2024 by Roshan Parihar
To sort the list of tuples in ascending order or increasing order in Python, use the sort()
function with 'key=lambda'
.
You can also use sorted()
and 'key=itemgetter()'
for sorting of tuples. Let’s find out the different methods with the examples given below here.
How to Sort List of Tuples Using sort() 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.
Example 1
1 2 3 4 5 6 7 8 |
#Define a 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]
.
Example 2
1 2 3 4 5 6 7 8 |
#Define a 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.
Using sorted() and key=lambda to Sort List of Tuples in 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.
Example 3
1 2 3 4 5 6 7 8 |
#Define a 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]
.
Example 4
1 2 3 4 5 6 7 8 |
#Define a 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
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)
.
Example 5
1 2 3 4 5 6 7 8 9 10 11 |
#Import Python library from operator import itemgetter #Define a 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.
Example 6
1 2 3 4 5 6 7 8 9 10 11 |
#Import Python library from operator import itemgetter #Define a 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.