Sorting List of Lists with First Element of Each Sub-List in Python
Last Updated :
06 Feb, 2024
In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list of lists by the first element of each sub-list. This process organizes the outer list based on the values of the first elements in its sub-lists. Here, we'll explore different methods to achieve this task.
Python Sorting List Of Lists By The First Element Of Each Sub-List
Below are some of the ways by which we can sort the list of lists by the first element of each sub-list in Python:
- Using the
sorted
Function with a Lambda Function - Using the
sort()
method with a Custom Comparator - Using List Comprehension with Sorted
- Using the
itemgetter
Function from the operator
Module - Using a Custom Function
Sorting List Of Lists by First Element Using sorted()
Function
In this approach, we use the sorted()
function with a lambda function as the key argument to sort the list based on the first element of each sub-list.
Python3
# Using the sorted() Function with a Lambda Function
list_of_lists= [[3, 'b'], [1, 'a'], [2, 'c']]
sorted_list= sorted(list_of_lists, key=lambda x: x[0])
#Display the sorted
print(sorted_list)
Output[[1, 'a'], [2, 'c'], [3, 'b']]
Sort 2-D List by First Element Using sort() Function
In this approach, we are using the sort()
method with a Custom Comparator as the key to sort the list in-place based on the first element of each sub-list.
Python3
#Using the sort() Method with a Custom Comparator
list_of_lists = [[3, 'b'], [1, 'a'], [2, 'c']]
list_of_lists.sort(key=lambda x: x[0])
#Display the sorted list
print(list_of_lists)
Output[[1, 'a'], [2, 'c'], [3, 'b']]
Python Sort List Of Lists by First Element Using List Comprehension
One of the way is to use List comprehension along with the sorted()
function to create a new sorted list based on the first element of each sub-list.
Python3
# Using List Comprehension with Sorted
list_of_lists = [[3, 'b'], [1, 'a'], [2, 'c']]
sorted_list = [x for x in sorted(list_of_lists, key=lambda x: x[0])]
#Display sorted list
print(sorted_list)
Output[[1, 'a'], [2, 'c'], [3, 'b']]
Python Sort 2-D List by First Element Using itemgetter()
Function
One of the ways is to use itemgetter function from the operator module as the key argument in the sorted() function to achieve sorting based on the first element of each sub-list.
Python3
# Using the itemgetter Function from the operator Module
from operator import itemgetter
list_of_lists = [[3, 'b'], [1, 'a'], [2, 'c']]
sorted_list= sorted(list_of_lists, key=itemgetter(0))
# Display sorted list
print(sorted_list)
Output[[1, 'a'], [2, 'c'], [3, 'b']]
Sorting 2-D List By First Element Using a Custom Function
In this method custom sorting function is defined, and this function is used as the key argument in the sorted()
function to sort the list based on the first element of each sub-list.
Python3
# Using a Custom Function
def custom_sort(sub_list):
return sub_list[0]
list_of_lists = [[3, 'b'], [1, 'a'], [2, 'c']]
sorted_list = sorted(list_of_lists, key=custom_sort)
#Display list in sorted order
print(sorted_list)
Output[[1, 'a'], [2, 'c'], [3, 'b']]
Similar Reads
Python | Sorting list of lists with similar list elements Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Le
5 min read
Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple - Python The task of sorting a list of tuples in Python based on the last element of each tuple is a common task when working with structured data. This involves arranging the tuples in increasing order according to their second or last value, often for easier data analysis or searching. For example, given a
2 min read
Python | Get first element of each sublist Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists. Examples: Input : [[1, 2], [3, 4, 5], [6, 7, 8, 9]] Output : [1, 3, 6] Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']] Output : ['x', 'm', 'a', 'u'] Â Approach #1 : List compre
3 min read
Convert List of String into Sorted List of Integer - Python We are given a list of string a = ['3', '1', '4', '1', '5'] we need to convert all the given string data inside the list into int and sort them into ascending order so the output list becomes a = [1, 1, 3, 4, 5]. This can be achieved by first converting each string to an integer and then sorting the
3 min read
Python program to insert an element into sorted list Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 min read