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 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
Python | Sort a tuple by its float element In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting. Examples: Input : tuple = [('lucky', '18.265'), ('nikhil', '1
3 min read
Get Second Largest Number in Python List Using Bubble Sort Finding the second-largest number in a list is a common programming task that involves sorting the list in ascending order. Bubble sort is a simple sorting algorithm that can be used for this purpose. In this article, we will explore the logic behind finding the second largest number using bubble so
3 min read
Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
3 min read