How to Flatten a List of Lists in Python: A Practical Guide with Examples How to Flatten a List of Lists in Python: A Practical Guide with Examples

Working with nested lists is a common task in Python programming. Sometimes, you may encounter a situation where you need to flatten a list of lists into a single flat list. Flattening a list simplifies data processing and makes it easier to perform various operations such as searching, sorting, or iterating over the elements. In this article, we will explore different methods to accomplish this task using Python.


Method 1: Using a Loop

The most straightforward approach to flatten a list of lists is by using a loop. We iterate over each element in the nested list and append it to a new flat list. Here's an example implementation:


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

Output:


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

Method 2: Using List Comprehension

Python's list comprehension provides a concise way to create a flat list from a list of lists. We can use a nested loop within a list comprehension to iterate over the elements and generate the flat list. Here's an example:


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

Output:


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

Method 3: Using itertools.chain()

The itertools module in Python provides a powerful function called `chain()` that can be used to flatten a list of lists. The `chain()` function takes multiple iterables as arguments and returns a single iterator that sequentially produces the items from each input iterable. We can convert this iterator into a flat list. Here's an example:


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

Output:


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

Method 4: Using Recursive Function

Another approach to flatten a list of lists is by using a recursive function. This method is particularly useful when dealing with deeply nested lists. The function iterates over each element in the nested list and recursively calls itself if an element is also a list. Here's an example implementation:


def flatten_list(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten_list(item))
else:
flat_list.append(item)
return flat_list
nested_list = [[1, 2, 3], [4, [5, 6], 7], [8, [9]]]
flat_list = flatten_list(nested_list)
print(flat_list)

Output:


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

Flattening a list of lists can be achieved using different methods in Python. You can choose the method that suits your specific needs and preferences. Whether you prefer a simple loop, list comprehension, itertools, or a recursive function, the goal remains the same: transforming a nested list into a flat list to simplify data processing.

Published on May 28, 2023

Tags: Python | tolist

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.