Last modified: Jan 03, 2025 By Alexander Williams
Python Flatten List of Lists: Complete Guide
Working with nested lists in Python is common, but sometimes you need to convert them into a single flat list. This process is called flattening, and there are several ways to accomplish this task.
Using List Comprehension
The simplest way to flatten a list of lists is using list comprehension. This method works well for lists with one level of nesting. Let's look at a basic example.
# Simple nested list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [item for sublist in nested_list for item in sublist]
print(flattened)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Using itertools.chain Method
Another efficient way to flatten lists is using the itertools.chain()
method. This method is particularly useful when dealing with multiple lists that need to be combined.
from itertools import chain
nested_list = [[1, 2], [3, 4], [5, 6]]
flattened = list(chain(*nested_list))
print(flattened)
[1, 2, 3, 4, 5, 6]
Recursive Approach for Deep Nesting
When dealing with deeply nested lists, a recursive approach is more appropriate. This method can handle any level of nesting in your lists.
def flatten_recursive(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten_recursive(item))
else:
flat_list.append(item)
return flat_list
# Deep nested list
deep_nested = [1, [2, 3, [4, 5]], [6, [7, 8]]]
result = flatten_recursive(deep_nested)
print(result)
[1, 2, 3, 4, 5, 6, 7, 8]
Using reduce Function
The reduce()
function from the functools module provides another elegant solution for flattening lists. This approach is particularly useful when working with multiple list operations.
from functools import reduce
from operator import concat
nested_list = [[1, 2], [3, 4], [5, 6]]
flattened = reduce(concat, nested_list)
print(list(flattened))
[1, 2, 3, 4, 5, 6]
Using numpy flatten
If you're working with numerical data, numpy
's flatten method can be very efficient, especially when dealing with large datasets.
import numpy as np
nested_array = np.array([[1, 2], [3, 4], [5, 6]])
flattened = nested_array.flatten()
print(flattened)
[1 2 3 4 5 6]
Performance Considerations
When choosing a flattening method, consider the size and structure of your data. List comprehension is fastest for simple nesting, while recursive approaches handle complex nesting better.
For small lists: List comprehension or itertools.chain is recommended for their simplicity and readability.
For large lists: numpy.flatten() offers better performance when working with numerical data.
For deeply nested lists: The recursive approach is the most versatile but may be slower for very large structures.
Common Pitfalls to Avoid
When flattening lists, be careful with empty lists and non-list elements. Always validate your input data and handle edge cases appropriately.
# Handling empty lists and mixed types
def safe_flatten(nested_list):
if not nested_list:
return []
return [item for sublist in nested_list
for item in (safe_flatten(sublist) if isinstance(sublist, list) else [sublist])]
mixed_list = [1, [], [2, 3, []], [4, [5, 6]]]
result = safe_flatten(mixed_list)
print(result)
Conclusion
Python offers multiple ways to flatten nested lists, each with its own advantages. Choose the method that best fits your specific use case, considering factors like data structure, performance needs, and code readability.