Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Sorting a List of Lists

Learn how to sort a list of lists in Python, including practical examples and code snippets. …


Updated June 24, 2023

Learn how to sort a list of lists in Python, including practical examples and code snippets. Sorting a List of Lists in Python

As a Python developer, you may encounter situations where you need to sort a list of lists. This can be useful in various applications, such as data analysis, scientific computing, or even game development. In this article, we’ll explore the concept of sorting a list of lists in detail, including step-by-step explanations and code examples.

Definition: A list of lists, also known as a nested list or multidimensional list, is a data structure that consists of one or more lists inside another list. For example:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Step-by-Step Explanation:

To sort a list of lists in Python, you can follow these steps:

Step 1: Flatten the List of Lists

First, you need to flatten the list of lists into a single list. You can use the itertools.chain function or a simple loop to achieve this:

import itertools

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

flattened_list = list(itertools.chain(*list_of_lists))
print(flattened_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Step 2: Sort the Flattened List

Next, you can sort the flattened list using the built-in sorted function:

sorted_list = sorted(flattened_list)
print(sorted_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Step 3: Reconstruct the List of Lists (Optional)

If you want to reconstruct the list of lists with the sorted elements, you can use a simple loop:

reconstructed_list = []
for i in range(0, len(sorted_list), 3):
    reconstructed_list.append(sorted_list[i:i+3])
print(reconstructed_list)  
# Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Alternative Approaches

You can also use the following approaches to sort a list of lists:

  • Using sorted with a custom key: If you want to sort the list based on specific criteria (e.g., tuples or dictionaries), you can pass a custom key function to the sorted function:
list_of_lists = [[('a', 1)], [('b', 2)], [('c', 3)]]
sorted_list = sorted(list_of_lists, key=lambda x: x[0])
print(sorted_list)  
# Output: [[('a', 1)], [('b', 2)], [('c', 3)]]
  • Using a sorting algorithm: If you want to implement a custom sorting algorithm (e.g., quicksort or mergesort), you can use a library like sortedcontainers:
from sortedcontainers import SortedList

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sorted_list = SortedList(list_of_lists)
print(sorted_list)  
# Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Conclusion

Sorting a list of lists in Python can be achieved using various approaches, including flattening the list, sorting the flattened list, and reconstructing the list of lists. You can also use custom key functions or implement specific sorting algorithms for more complex scenarios. By following these steps and examples, you should be able to efficiently sort your nested lists and work with them in Python.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp