Open In App

Sorting List of Lists with First Element of Each Sub-List in Python

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads