This article is part of in the series
Published: Tuesday 10th June 2025

python flatten list blog banner image

Working with lists in Python is a core skill for any programmer, from data scientists to web developers. One of the most common challenges is flattening a list. This challenge is very common if you are regularly dealing with nested structures. In this detailed guide, we will explain the many ways to flatten a list in Python. Whether you're a beginner or a seasoned Python expert, this guide will serve as your trusted reference.

It goes without saying but still, for our new visitors: we will be starting with the meaning of flattening a list, importance of flattening, different methods to do that, and wrap up with best practices. This is the structure we follow at PythonCentral so that you don't have any doubts after reading this guide. Ready? Get. Set. Learn!

What is Flattening a List

To flatten a list means to transform a nested list i.e., a list of lists, into a one-dimensional list. For example, let us look at a nested list:

nested_list = [[1, 2], [3, 4], [5, 6]]

The flattened version will look like this:

[1, 2, 3, 4, 5, 6]

Why Is Flattening Lists Important

Even if you do not work that much with nested lists, we recommend you learn about flattening lists because it helps you in:

  • Data preprocessing for machine learning
  • Database operations where normalized data needs to be converted
  • File parsing where data is loaded as nested lists and many more applications.

How to Flatten Lists in Python

Let us learn 5 different methods to flatten a list.

Method 1: Using List Comprehension

This is the most basic way to flatten a 2D list:

flattened = [item for sublist in nested_list for item in sublist

This script is concise and readable. For shallow lists, this works very fast. But the only concern is that this method works only for 2D lists.

Method 2: Using "itertools.chain"

Now let us use "itertools.chain" from Python's standard library.

import itertools

flattened = list(itertools.chain.from_iterable(nested_list))

This method is very efficient and memory-friendly with an added advantage of being elegant for shallow lists. The problem is that this method is not suitable for deeply nested lists.

Method 3: Using Recursion for Deeply Nested Lists

If your list is arbitrarily nested, it is time to use recursion. Let us learn how to use that:

def flatten_recursive(lst):
flat_list = []
for item in lst:
if isinstance(item, list):
flat_list.extend(flatten_recursive(item))
else:
flat_list.append(item)
return flat_list

With recursion, you can handle deeply nested lists. But be careful about hitting the recursion limits for extremely deep structures.

Method 4: Using "numpy.flatten" and "numpy.ravel"

If your data involves numerals, you can use NumPy. Here is how you can do that:

import numpy as np

array = np.array(nested_list)
flattened = array.flatten()
# or
flattened = array.ravel()

NumPy is optimized for performance and works beautifully with numerical arrays. Since it requires NumPy, you have to brush up on your NumPy skills first (and we have you covered in the related links section). And this method works only on arrays and not on generic lists.

Method 5: Using "functools.reduce"

This method usses functools.reduce. The main advantage is that this method is compact and very functional.

from functools import reduce

flattened = reduce(lambda x, y: x + y, nested_list)

The problem with this method is, if you use it on complex lists, this script is not readable-friendly. Chances are great that this may break when you work with deeply nested or non-list elements.

Which Method Should You Choose

We get it. It can get complicated to choose one of the five methods we explained above. To help you make a decision, let us make it easier for you. Here are a few pointers you should keep in mind when it comes to decision making time:

  • Recursion is the choice when you want to work on deep nested lists.
  • NumPy is the fastest method followed by list comprehension and "itertools.chain" methods.
  • If memory usage is a constraint, you can consider list comprehension and "itertools.chain" methods.

Best Practices and Troubleshooting instructions

  • Prefer list comprehension or itertools.chain for flat 2D lists.
  • Recursion or libraries like "more_itertools" are the best for deeper structures.
  • Always validate the input before flattening to avoid type errors.
  • If you face "TypeError", it means you are trying to flatten non-iterable elements.
  • "ValueError" shows up when you use Using NumPy flatten on irregular nested lists.

Wrapping Up

The process to flatten a list in Python is a fundamental skill for any developer. From simple list comprehensions to recursive deep flattening, Python offers a solution for every complexity level. Use this guide as your step-by-step tutorial for using the python flatten list operation.

Related Links

NumPy where(): Using Conditional Array Operations

How to Slice Lists/Arrays and Tuples in Python

Append In Python: How to Use Python List Append