Recently, I was working on a project where I had to organize student test scores stored in a list of lists. Each sublist contained the student’s name and score.
At first, I thought sorting this kind of nested list would be tricky. But after experimenting with Python’s built-in functions, I realized there are several simple ways to do it.
In this tutorial, I’ll show you five easy methods to sort a list of lists in Python. I’ll use real-world examples so you can apply them directly to your own projects.
Method 1: Use Python’s sort() method
The sort() is a built-in method in Python that sorts the elements of a list in place. By using a sort() along with a lambda function or other callable object.
Code
list_of_lists = [['New York', 20000000, 800, 30], ['Los Angeles', 15000000, 600, 80], ['Chicago', 9000000, 500, 40]]
# Sort based on the third element of each sublist
list_of_lists.sort(key=lambda x: x[3])
print(list_of_lists)Output
[['New York', 20000000, 800, 30], ['Chicago', 9000000, 500, 40], ['Los Angeles', 15000000, 600, 80]]I executed the above example code and added the screenshot below.

This method sorts a list of lists in place based on a specified element from each sublist using the sort() method with a lambda key.
Method 2: Use itemgetter with the sorted() method
The sorted() function in Python sorts the elements of a list. However, unlike sort(), sorted() returns a new sorted list instead of modifying the original list.
Code
from operator import itemgetter
list_of_lists = [['Urban', 10000000, 200, 3], ['Rural', 5000000, 150, 8], ['Suburban', 8000000, 300, 4]]
# Sort based on the first element of each sublist
sorted_list = sorted(list_of_lists, key=itemgetter(1))
print(sorted_list)In the above example, we’ve sorted a list of lists using the sorted() function along with the itemgetter function from the operator module in Python.
Output
[['Rural', 5000000, 150, 8], ['Suburban', 8000000, 300, 4], ['Urban', 10000000, 200, 3]]I executed the above example code and added the screenshot below.

It sorts the list based on the first element of each sublist. The itemgetter(0) function in Python specifies that the sorting key should be the first element of each sublist.
Method 3: Use sorted() with lambda function
Lambda functions offer a concise way to define small, anonymous functions in Python.
By combining lambda functions with the sorted() function, we can specify custom sorting criteria based on the elements of the sublists in a list of lists in Python.
Code
employee_data = [['John', 35, 'Manager'], ['Emily', 28, 'Engineer'], ['Michael', 40, 'Analyst']]
# Sort based on the age (2nd element) of each employee
sorted_employee_data = sorted(employee_data, key=lambda x: x[1])
print(sorted_employee_data)The code utilizes the sorted() function and a lambda function to sort a list of lists based on the first element of each sublist in Python.
Output
[['Emily', 28, 'Engineer'], ['John', 35, 'Manager'], ['Michael', 40, 'Analyst']]I executed the above example code and added the screenshot below.

The lambda function lambda x: x[0] specifies that the sorting key should be the first element of each sublist.
Method 4: Use List Comprehension with the sorted() method
List comprehension in Python provides a compact and elegant way to create lists in Python.
When combined with the sorted() function, list comprehension in Python enables us to sort a list of lists based on specific conditions defined within the comprehension.
Code
customer_data = [[1, 3, 500], [2, 5, 200], [3, 7, 400]]
# Sort based on the purchase amount (third element) of each customer using list comprehension
sorted_customer_data = sorted(customer_data, key=lambda x: max([element for element in x]))
print(sorted_customer_data)sorted(list_of_lists, key=lambda x: max([element for element in x]))Output
[[2, 5, 200], [3, 7, 400], [1, 3, 500]]I executed the above example code and added the screenshot below.

The list comprehension in Python extracts all elements from each sublist x, and max() is applied to find the maximum element.
Method 5: Use map() with sorted()
The map() function applies a given Python function to each item of an iterable (such as a list) and returns an iterator of the results.
By utilizing map() along with the sorted() function, we can apply a custom function to each element of the list of lists in Python.
Code
list_of_lists = [[3, 5, 1], [8, 2, 9], [4, 7, 6]]
# Sorting based on minimum element in each sublist
sorted_list = sorted(list_of_lists, key=lambda x: min(map(lambda y: y, x)))
print(sorted_list)In this code, we’ve used the map() function and the sorted() function to sort a list of lists in Python based on the minimum element in each sublist.
sorted(list_of_lists, key=lambda x: min(map(lambda y: y, x)))Output:
[[3, 5, 1], [8, 2, 9], [4, 7, 6]]I executed the above example code and added the screenshot below.

Python’s map() function applies the lambda function lambda y: y to each element of the sublist, and min() is then used to find the minimum element.
Method 6: Use built-in Functions
We will create a custom function to sort the list of lists in Python, which will take parameters as a list of lists. Then, we will use the bubble sort approach to sort the list of lists without using any built-in method of Python.
Code
def sort_list_of_lists(list_of_lists):
n = len(list_of_lists)
for i in range(n):
for j in range(0, n-i-1):
if list_of_lists[j] > list_of_lists[j+1]:
list_of_lists[j], list_of_lists[j+1] = list_of_lists[j+1], list_of_lists[j]
my_list_of_lists = [[3, 5], [1, 2], [4, 6], [2, 3], [5, 1]]
sort_list_of_lists(my_list_of_lists)
print(my_list_of_lists) The provided code implements the bubble sort algorithm to sort a list of lists in ascending order based on their first elements in Python.
Output
[[1, 2], [2, 3], [3, 5], [4, 6], [5, 1]]I executed the above example code and added the screenshot below.

In each outer loop iteration, the algorithm compares adjacent sublists and swaps them if they are out of order, moving the largest element towards the end of the list. This process repeats until the entire list is sorted in Python.
I explored built-in functions like sort() and sorted() along with lambda functions, list comprehension, and the map() function to achieve custom sorting criteria. A custom sorting function using the bubble sort algorithm was also demonstrated for those seeking an alternative approach.
You may also like to read these articles:
- 3D Arrays in Python
- Print Duplicate Elements in an Array in Python
- Convert Array to Set in Python
- Count Occurrences in Python Arrays

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.