How do I make a flat list out of a list of lists?
Categories:
Flattening Lists: How to Convert a List of Lists into a Flat List in Python

Learn various Pythonic techniques to transform a nested list (list of lists) into a single, flat list, enhancing data processing and readability.
Working with data often involves dealing with nested structures. In Python, a common scenario is having a 'list of lists' (also known as a multidimensional array or nested list) that you need to convert into a single, 'flat' list. This process, known as flattening, is crucial for many data manipulation tasks, from preparing data for analysis to simplifying iteration. This article explores several effective methods to achieve this, ranging from simple loops to more advanced functional programming techniques.
Understanding the Problem: Nested vs. Flat Lists
Before diving into solutions, let's clarify what we mean by nested and flat lists. A nested list is a list where some or all of its elements are themselves lists. A flat list, on the other hand, contains only non-list elements. The goal is to take the elements from all inner lists and collect them into one continuous list.
graph TD A[Nested List] --> B{"Flattening Process"} B --> C[Flat List] subgraph Nested List Example NL1["[1, 2]"] NL2["[3, 4]"] NL3["[5, 6]"] NL_Parent["[[1, 2], [3, 4], [5, 6]]"] NL_Parent --> NL1 NL_Parent --> NL2 NL_Parent --> NL3 end subgraph Flat List Example FL["[1, 2, 3, 4, 5, 6]"] end NL_Parent --> B B --> FL
Conceptual diagram illustrating the transformation from a nested list to a flat list.
Method 1: Using Nested Loops (The Basic Approach)
The most straightforward way to flatten a list of lists is by using nested for
loops. This method is easy to understand and implement, making it a good starting point, especially for beginners. You iterate through the outer list, and for each inner list, you iterate through its elements and append them to a new, flat list.
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]
Flattening a list of lists using nested for loops.
Method 2: List Comprehension (Pythonic and Concise)
List comprehensions offer a more concise and often more efficient way to achieve the same result as nested loops. They are a powerful feature in Python for creating lists based on existing iterables. The syntax for flattening a list of lists using list comprehension mirrors the nested loop structure but in a single line.
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]
Flattening a list of lists using a nested list comprehension.
Method 3: Using sum()
(Concatenation for Numeric Lists)
The built-in sum()
function can be used to flatten a list of lists, particularly when the inner lists contain numeric types. When sum()
is called with a list of lists and an empty list []
as the starting value, it concatenates all the inner lists together. This method is elegant but should be used with caution.
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat_list = sum(nested_list, [])
print(flat_list)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Flattening a list of lists using the sum()
function.
sum()
can be significantly slower for large lists compared to list comprehensions or itertools.chain()
, as it creates a new list in each iteration. It's also primarily intended for numeric summation, so using it for list concatenation might be considered less idiomatic by some.Method 4: Using itertools.chain()
(Efficient for Iterators)
For highly efficient flattening, especially with large datasets or when working with iterators, the itertools.chain()
function is an excellent choice. It treats successive iterables as a single sequence, avoiding the creation of intermediate lists and thus being very memory-efficient.
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]
Flattening a list of lists using itertools.chain()
.
*
operator (unpacking operator) is used here to unpack the nested_list
into individual arguments for itertools.chain()
. This method is generally the most performant and memory-efficient for flattening.