How to Sort List of Tuples in Ascending Order in Python

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.

Output

[(1, 2), (2, 3), (3, 5), (6, 3)]

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].

Output

[(1, 2), (6, 3), (2, 3), (3, 5)]

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.

Output

[(1, 2), (2, 3), (3, 5), (6, 3)]

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].

Output

[(1, 2), (6, 3), (2, 3), (3, 5)]

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).

Output

[(1, 2), (2, 3), (3, 5), (6, 3)]

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.

Output

[(1, 2), (6, 3), (2, 3), (3, 5)]

As a result, it gives the same result of sorted elements according to the 2nd items of each tuple in a list.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.