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

H

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:

[code]
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)
[/code]

Output:
[code]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[/code]

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:

[code]
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)
[/code]

Output:
[code]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[/code]

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:

[code]
import itertools

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

print(flat_list)
[/code]

Output:
[code]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[/code]

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:

[code]
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)
[/code]

Output:
[code]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[/code]

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.

About the author

By Jamie

My Books