How to Flatten a List of Lists in Python

Avatar

By squashlabs, Last Updated: October 15, 2023

How to Flatten a List of Lists in Python

Flattening a list of lists is a common operation in Python, and there are several ways to achieve this. In this answer, we will explore two popular methods: using nested list comprehensions and using the itertools.chain.from_iterable() function.

Method 1: Nested List Comprehensions

One way to flatten a list of lists in Python is by using nested list comprehensions. This method is concise and efficient, making it a popular choice among Python developers.

Here’s an example of how to use nested list comprehensions to flatten a list of lists:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [item for sublist in nested_list for item in sublist]
print(flattened_list)

Output:

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

In the example above, we have a nested list nested_list containing three sublists. The nested list comprehension item for sublist in nested_list for item in sublist iterates over each sublist in nested_list and then iterates over each item in the sublist, effectively flattening the list.

Related Article: How To Convert a Dictionary To JSON In Python

Method 2: itertools.chain.from_iterable()

Another way to flatten a list of lists in Python is by using the itertools.chain.from_iterable() function from the itertools module. This method is more explicit and readable, especially for larger and more complex lists.

Here’s an example of how to use itertools.chain.from_iterable() to flatten a list of lists:

import itertools

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = list(itertools.chain.from_iterable(nested_list))
print(flattened_list)

Output:

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

In the example above, we import the itertools module and then use the chain.from_iterable() function to flatten the nested_list. The function chain.from_iterable() takes the nested list as an argument and returns an iterator that produces the flattened list. We convert the iterator to a list using the list() function for easier printing and manipulation.

Best Practices

When flattening a list of lists in Python, consider the following best practices:

1. Use nested list comprehensions for simple and concise code. This method is ideal for small to medium-sized lists.

2. Use itertools.chain.from_iterable() for larger or more complex lists. This method is more explicit and allows for easier customization or modification.

3. If you only need to iterate over the flattened list and don’t need a new list object, consider using a generator expression instead of a list comprehension. This can save memory and improve performance, especially for large lists.

4. If you need to flatten a list of lists to a specific depth, consider using a recursive function or a third-party library like flatten-list.

Related Article: How to Sort a Dictionary by Key in Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Remove a Key from a Python Dictionary

Removing a key from a Python dictionary is a common task in programming. This guide provides step-by-step instructions on how to achieve this using the del statement,... read more

How to Remove an Element from a List by Index in Python

A guide on removing elements from a Python list by their index. Methods include using the 'del' keyword, the 'pop()' method, the 'remove()' method, list comprehension,... read more

How to Solve a Key Error in Python

This article provides a technical guide to resolving the KeyError issue in Python. It covers various methods such as checking if the key exists before accessing it,... read more

How to Check If Something Is Not In A Python List

This article provides a guide on using the 'not in' operator in Python to check if an item is absent in a list. It covers the steps for using the 'not in' operator, as... read more

How to Add New Keys to a Python Dictionary

Adding new keys and their corresponding values to an existing Python dictionary can be achieved using different methods. This article provides a guide on two popular... read more

How to Read a File Line by Line into a List in Python

Reading a file line by line into a list in Python is a common task for many developers. In this article, we provide a step-by-step guide on how to accomplish this using... read more