How to Sort List of Tuples in Ascending Order in Python

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

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

Example 2

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.

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

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

Example 4

Output

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

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

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.

Example 6

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.