Open In App

Sort a list according to the second element of sublist - Python

Last Updated : 09 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in ascending order. Let's explore various methods to achieve this efficiently.

Using itemgetter()

itemgetter() function from Python’s operator module is a highly efficient way to sort lists when dealing with sublists or tuples. It avoids the overhead of defining a lambda function, making it ideal for larger datasets.

Python
from operator import itemgetter
a = [[1, 3], [2, 1], [4, 2], [3, 5]]

a = sorted(a, key=itemgetter(1)) 
print(a)  

Output
[[2, 1], [4, 2], [1, 3], [3, 5]]

Explanation: sorted(a, key=itemgetter(1)) sorts the list a based on the second element of each sublist. The itemgetter(1) function specifies that the sorting should use the value at index 1 of each sublist.

Using sorted()

sorted() function returns a new sorted list from the given iterable. When combined with a lambda, it allows custom sorting logic such as sorting by the second element.

Python
a = [[1, 3], [2, 1], [4, 2], [3, 5]]

a = sorted(a, key=lambda x: x[1])  
print(a)

Output
[[2, 1], [4, 2], [1, 3], [3, 5]]

Explanation: sorted(a, key=lambda x: x[1]) sorts the list a by the second element of each sublist, using a lambda function to specify that the sorting should be done based on the value at index 1 of each sublist.

Using sort() Method

If we don’t need a new list and prefer to sort the list in place, we can use the sort() method. It directly modifies the list we’re working with instead of creating a new one.

Python
a = [[1, 3], [2, 1], [4, 2], [3, 5]]

a.sort(key=lambda x: x[1]) 
print(a)

Output
[[2, 1], [4, 2], [1, 3], [3, 5]]

Explanation: a.sort(key=lambda x: x[1]) sorts the list a in place by the second element of each sublist. The lambda function specifies that the sorting should be done based on the value at index 1 of each sublist.

Using loop

If you want to sort the list without using any built-in sorting functions, you can implement a basic Bubble Sort algorithm or any other sorting logic manually. This is useful for learning purposes or in environments where inbuilt functions are restricted.

Python
a = [[1, 3], [2, 1], [4, 2], [3, 5]]

n = len(a)
for i in range(n):
    for j in range(0, n-i-1):
        if a[j][1] > a[j+1][1]:
            a[j], a[j+1] = a[j+1], a[j]

print(a)

Output
[[2, 1], [4, 2], [1, 3], [3, 5]]

Explanation: Outer loop runs n times, bubbling the largest second element up each pass. The inner loop compares adjacent sublists, skipping the last i sorted elements. If a[j][1] > a[j+1][1], the sublists are swapped, moving the larger element toward the end.

Related Articles


Next Article
Practice Tags :

Similar Reads