Sort a list according to the second element of sublist - Python
Last Updated :
09 May, 2025
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
Similar Reads
Python program to Sort a list according to the second element of sublist 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 as
3 min read
Sort a List According to the Length of the Elements - Python We are given a list of strings and our task is to sort the list based on the length of each string. For example, if the input is ["Python", "C", "Java", "JavaScript", "Go"], the output should be ["C", "Go", "Java", "Python", "JavaScript"] since shorter strings come first.Using sorted() sorted() func
3 min read
Sort a List of Tuples by Second Item - Python The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)],
2 min read
Sort a List of Tuples by Second Item - Python The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)],
2 min read
Sort a list in Python without sort Function Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9]. Letâs explore different methods of sorting a list without using built-in sort func
3 min read
Sort a list in Python without sort Function Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9]. Letâs explore different methods of sorting a list without using built-in sort func
3 min read
Sort a list according to the second element of sublist – Python
min read