Mastering List Flattening in Python: From Nested to One-Dimensional
Introduction to Flattening Lists in Python
If you've ever worked with nested lists in Python, you know they can be tricky to handle. Flattening—the process of converting a list of lists into a single, one-dimensional list—is a common operation. For example, imagine you have a matrix of numbers stored as a list of rows:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5]
]Your goal is to flatten it into one list:
[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]Python offers several ways to achieve this, each with its own strengths. In this guide, we'll explore all major methods, from simple loops to advanced functional tools and even NumPy for large datasets.
The Classic for Loop Approach
The most intuitive way to flatten a list of lists is to use a for loop. You create a new empty list, then iterate over each sublist, extending the new list with its elements. You can use either .extend() or the augmented assignment operator +=:
flat_list = []
for sublist in matrix:
flat_list.extend(sublist)
# Or: flat_list += sublistThis method is straightforward and easy to understand, making it ideal for beginners. It also performs well for moderate-sized lists.
Flattening with a List Comprehension
For a more concise solution, a list comprehension can flatten lists in a single line. The trick is to use two for clauses—one for the outer list and one for the inner elements:
flat_list = [item for sublist in matrix for item in sublist]This reads as: for each sublist in matrix, for each item in that sublist, add the item to the new list. List comprehensions are both readable and efficient, often faster than explicit loops in CPython.
Using itertools.chain() for Simplicity
The Python standard library provides itertools.chain(), which is designed exactly for this purpose. It takes multiple iterables and chains them together:
from itertools import chain
flat_list = list(chain.from_iterable(matrix))
# Or: flat_list = list(chain(*matrix))The from_iterable variant is cleaner when you already have a list of lists. This method is highly readable and memory-efficient because chain returns an iterator.
Flattening with functools.reduce()
If you're comfortable with functional programming, functools.reduce() can also flatten lists. You apply list.__add__ repeatedly to join sublists:

from functools import reduce
flat_list = reduce(lambda x, y: x + y, matrix)While this works, be cautious: reduce creates many intermediate lists, so it can be slow for large data. It's more of a functional curiosity than a recommended approach.
Handling Deeply Nested Lists with Recursion
What if your lists are nested arbitrarily deep (e.g., [1, [2, [3, 4]], 5])? The previous methods only work for one level of nesting. To handle multiple levels, you need a recursive flatten function:
def flatten_deep(nested_list):
result = []
for item in nested_list:
if isinstance(item, list):
result.extend(flatten_deep(item))
else:
result.append(item)
return resultThis function checks each element; if it's a list, it recurses; otherwise, it appends the element. You can also write an iterative version using a stack, but recursion is elegantly simple.
NumPy's .flatten() for Data Science
If you're working with numerical data, the NumPy library offers a highly optimized .flatten() method:
import numpy as np
flat_array = np.array(matrix).flatten()This converts the matrix to a NumPy array and then flattens it into a 1D array. It's extremely fast for large datasets, but it requires NumPy and returns a NumPy array, not a list. Use .tolist() if you need a standard Python list.
Conclusion: Choosing the Right Method
Flattening a list of lists in Python is simple once you know the tools. For quick one-liners, list comprehensions or itertools.chain() are great. For deep nesting, write a recursive function. And for massive numerical data, NumPy is unbeatable. Pick the method that fits your specific use case, and you'll be flattening like a pro in no time.
Related Articles
- Inside the Python Security Response Team: Governance, Growth, and How to Get Involved
- Go 1.26 Type Checker: Cycle Detection Overhaul Sets Stage for Future Improvements
- Mastering GDB Source-Tracking Breakpoints: Your Questions Answered
- Mastering Stack Allocation in Go: Q&A on Boosting Performance
- Apple Issues Urgent Warning: Mac Mini and Mac Studio Supplies to Remain Tight for Months Amid AI-Driven Demand Surge
- Modernize Your Go Code with go fix: A Q&A Guide
- How to Get Involved in Google Summer of Code 2026: A Step-by-Step Guide for Student Developers
- NVIDIA's Nemotron 3 Nano Omni Model Unifies Multimodal AI with 9x Efficiency Leap