Flatten List of Lists in Python
In Python, you can flatten a nested list (list of lists) in several ways. Here are 5 common approaches:
(1) Using list comprehension
A list comprehension is a concise way to create a new list based on existing iterables (like lists, tuples, or sets).
To flatten a list of lists using list comprehension, you can use a nested for
loop with the comprehension.
For example:
nested_list = [[1, 2, 3], [4, 5], [6, 7]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list) # Output: [1, 2, 3, 4, 5, 6, 7]
Here’s how it works:
nested_list
: This is the list you want to flatten.
for sublist in nested_list
: This outer loop iterates through each sublist in the nested_list
.
for item in sublist
: This inner loop iterates through each item in the current sublist
.
item
: Each item
is added to the flat_list
.
List comprehension is a concise and Pythonic way to flatten a list of lists in Python.
(2) Using itertools.chain
itertools.chain
is a function from Python’s itertools
module that is used to combine multiple iterables into a single iterable.
It is an efficient method to flatten nested lists because it avoids creating intermediate lists and processes elements lazily. It is particularly well-suited for handling large datasets or situations where minimizing memory usage is crucial.
Here’s how you can do it:
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]
Here is how the above code works:
*nested_list
: The unpacking operator (*
) unpacks the nested list into its individual sublists. For example, nested_list
is [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
, so *nested_list
becomes [1, 2], [3, 4, 5], [6, 7, 8, 9]
.
itertools.chain(*nested_list)
: The itertools.chain()
function takes these unpacked sublists and combines them into a single iterator
.
Alternative : If you prefer not to use unpacking (*
), you can achieve the same result by using itertools.chains.from_iterable(nested_list)
, which directly processes the nested structure without the need for *
.
list()
: The list()
function converts the resulting iterator
into a flattened list, producing [1, 2, 3, 4, 5, 6, 7, 8, 9]
.
(3) Using nested loop
To flatten a list of lists using a nested loop, you can iterate over each sublist in the main list and then over each element in the sublist and append it to a new list.
For example:
nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
# Initialize an empty list to store flattend elements
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]
(4) Using NumPy
If your nested list contains numerical data and requires high performance, NumPy
provides an efficient solution for flattening.
For example:
import numpy as np
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Convert to numpy array
array = np.array(matrix)
# Flatten the list
flat_array = array.flatten()
# Convert back to list (if needed)
flat_list = flat_array.tolist()
print(flat_array) # Output: [1 2 3 4 5 6 7 8 9]
print(flat_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
(5) Using a recursive function (for deeply nested lists)
A recursive function is a function that calls itself to solve a problem.
Flattening a list of lists using a recursive function involves defining a function that processes each element in the list: if the element is itself a list, the function calls itself to flatten it; otherwise, the element is directly added to the result.
Here is an example:
def flatten_list(nested_list):
flat_list = []
for element in nested_list:
# If the element is a list, recursively flatten it
if isinstance(element, list):
# Add the flattened sublist to the main flat list
flat_list.extend(flatten_list(element))
else:
# If the element is not a list, add it directly to the flat list
flat_list.append(element)
return flat_list
nested_list = [[1, 2], [3, [4, 5]], 6]
print(flatten_list(nested_list)) # Output: [1, 2, 3, 4, 5, 6]
Choosing the right method
Use list comprehension if you need a simple and concise way to flatten a list.
Use itertools.chain
if you are working with large datasets where performance and memory efficiency are important.
Use nested loops when you want a simple and clear solution, and performance is not a primary concern.
Use NumPy
if your data is numerical and performance is a key factor.
Use a recursive function if the list has multiple levels of nesting (deeply nested structure).