Introduction
In Python programming, it’s common to work with lists that contain other lists — known as nested lists. These structures can be useful for organizing data, but are sometimes difficult to process when you need a simple, one-dimensional list. Converting a nested list into a flat list (also called flattening) is an essential skill for data manipulation, especially in data science, machine learning, and API development.
In this article, we’ll explore multiple methods for flattening nested lists in Python, ranging from simple loops to utilizing libraries such as itertools and NumPy. Each method has its advantages, and we’ll also include a comparison table at the end for quick reference.
What Is a Nested List?
A nested list is a list that contains other lists as its elements. For example:
nested_list = [[1, 2, [3, 4]], [5, 6], [7, [8, 9]]]
Here, some elements inside nested_list are lists themselves. Our goal is to create a new list that contains all the elements as a single flat list:
flat_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
1. Using Loops
One of the simplest ways to flatten a nested list is by using loops — either a single loop or a nested loop.
Example:
def flatten_list(nested_list):
flat = []
for sublist in nested_list:
for item in sublist:
flat.append(item)
return flat
print(flatten_list([[1, 2], [3, 4], [5, 6]]))
Output:
[1, 2, 3, 4, 5, 6]
✅ Best for: Simple 2D lists where the nesting is only one level deep.
2. Using Recursion
When the list has multiple levels of nesting, recursion is a powerful way to flatten it.
Example:
def flatten_recursive(nested_list):
flat = []
for item in nested_list:
if isinstance(item, list):
flat.extend(flatten_recursive(item))
else:
flat.append(item)
return flat
print(flatten_recursive([[1, [2, [3, 4]]], [5, 6]]))
Output:
[1, 2, 3, 4, 5, 6]
Best for: Deeply nested lists with unknown depth.
3. Using itertools.chain()
The itertools module provides a fast and efficient way to flatten lists, but it only works well for one-level nested lists.
Example:
import itertools
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = list(itertools.chain.from_iterable(nested_list))
print(flat_list)
Output:
[1, 2, 3, 4, 5, 6]
Best for: Large datasets with one level of nesting (high performance).
4. Using List Comprehension
List comprehensions are a concise and readable way to flatten lists in Python.
Example:
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list)
Output:
[1, 2, 3, 4, 5, 6]
Best for: 2D lists when you want cleaner, more Pythonic code.
5. Using NumPy
If you’re working with numeric data, NumPy provides an efficient way to flatten lists using the flatten() or ravel() methods.
Example:
import numpy as np
nested_list = [[1, 2, 3], [4, 5, 6]]
array = np.array(nested_list)
flat_list = list(array.flatten())
print(flat_list)
Output:
[1, 2, 3, 4, 5, 6]
Best for: Numerical data and large lists that need fast processing.
6. Using sum() Function
Python’s built-in sum() function can also flatten a list, though it’s not recommended for deep or large lists due to performance limitations.
Example:
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = sum(nested_list, [])
print(flat_list)
Output:
[1, 2, 3, 4, 5, 6]
Best for: Quick and small 2D lists.
Comparison Table of All Methods
| Method | Handles Deep Nesting | Performance | Best Use Case | Requires Library | Code Example |
|---|
| Loop (Nested) | No | Fast for small lists | Simple 2D lists | No | for sublist in nested_list: |
| Recursion | Yes | Moderate | Deeply nested lists | No | if isinstance(item, list): |
| itertools.chain() | No | Very fast | Flattening one-level lists | Yes (itertools) | chain.from_iterable() |
| List Comprehension | No | Fast | Clean 2D flattening | No | [x for y in list for x in y] |
| NumPy.flatten() | No (unless array) | Very fast | Numeric datasets | Yes (numpy) | np.array(...).flatten() |
| sum() Function | No | Slow for large lists | Small lists | No | sum(list, []) |
Summary
Flattening a nested list in Python can be done in several ways — from simple loops and list comprehensions to using recursion, itertools, and NumPy. For shallow lists, list comprehensions or itertools are the most efficient and Pythonic. For deeply nested lists, recursion is your best bet. If you’re dealing with large numeric datasets, NumPy is the most efficient and scalable solution.
By mastering these different approaches, you’ll be well-prepared to handle complex data structures and make your Python code both efficient and readable. Whether you're building a data pipeline, working with JSON, or preparing input for machine learning models, knowing how to flatten lists effectively is an essential Python skill for eve