Last modified: Nov 10, 2026

Python Flatten a Nested List: Easy Guide

Working with data in Python often leads to lists inside other lists. These structures are called nested lists. Flattening a nested list means converting it into a single, one-dimensional list.

This guide shows you the best ways to flatten lists in Python. We will cover simple loops, list comprehensions, built-in modules, and recursion. Every method includes clear examples and outputs for beginners.

What Is a Nested List in Python?

A nested list is a list that contains other lists as its elements. Think of it like a box holding smaller boxes inside it. You might get nested lists when reading CSV files, parsing JSON data, or working with matrices.

For example, [[1, 2], [3, 4]] is a nested list. When you flatten it, you get [1, 2, 3, 4]. Before modifying complex structures, it helps to know how to get the length of a list in Python so you can track your data size.

Method 1: Using a Nested For Loop

The easiest way to understand flattening is by using two for loops. The outer loop grabs each sublist. The inner loop grabs each item from that sublist and adds it to a new list.

We use the append() method to add items one by one to our flat list. This approach is very readable and great for beginners.

# Define a 2D nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8]]

# Create an empty list to store flat results
flat_list = []

# Loop through each sublist
for sublist in nested_list:
    # Loop through each item in the sublist
    for item in sublist:
        flat_list.append(item)

print(flat_list)
[1, 2, 3, 4, 5, 6, 7, 8]

This method works reliably for any two-dimensional list. However, it takes several lines of code to write.

Method 2: Using List Comprehension

List comprehension offers a shorter and faster way to write the nested loop above. It puts the entire logic into a single line of code. Many Python developers prefer this style for its clean syntax.

The order of the loops stays the exact same as in the standard loop method. You write the outer loop first, followed by the inner loop.

# Define a nested list of strings
words = [["apple", "banana"], ["cherry"], ["date", "fig"]]

# Flatten using a single-line list comprehension
flat_words = [word for sublist in words for word in sublist]

print(flat_words)
['apple', 'banana', 'cherry', 'date', 'fig']

List comprehension runs faster than standard loops in Python. Keep in mind that it only works for lists that are one level deep.

Method 3: Using itertools.chain()

Python has a built-in module called itertools designed for fast iteration. You can use the chain() function from this module to link sublists together into one sequence.

To pass all sublists at once, use the unpacking operator * before the list name. Then wrap the result in the list() function to get a standard Python list back.

import itertools

# Define a nested list of numbers
matrix = [[10, 20], [30, 40], [50, 60]]

# Unpack sublists and chain them together
flat_matrix = list(itertools.chain(*matrix))

print(flat_matrix)
[10, 20, 30, 40, 50, 60]

This technique is very fast and memory efficient. It is ideal when you are working with large datasets. If you are only combining two simple lists instead of many nested ones, you can also explore how to concatenate two lists in Python using basic operators.

Method 4: Using the sum() Function

You can also flatten a shallow nested list using the built-in sum() function. Normally, this function adds numbers together. However, if you pass an empty list [] as the second argument, it joins sublists instead.

# Define a simple nested list
pairs = [[1, 2], [3, 4], [5, 6]]

# Join sublists using sum with an empty list start value
flat_pairs = sum(pairs, [])

print(flat_pairs)
[1, 2, 3, 4, 5, 6]

While this trick is clever and short, avoid using it on huge lists. Python creates a brand new list at every step, which slows down your program.

Method 5: Flattening Deeply Nested Lists with Recursion

All previous methods only work when lists are nested one level deep. What if you have lists inside lists inside other lists? For irregular or deeply nested lists, you need a recursive function.

Recursion happens when a function calls itself. We can use the isinstance() function to check if an item is a list. If it is, the function calls itself on that item. Otherwise, it adds the item to the final result.

# Define a custom recursive function
def flatten_deep(nested_data):
    result = []
    for element in nested_data:
        # Check if the current element is a list
        if isinstance(element, list):
            # Recursively flatten the sublist and extend result
            result.extend(flatten_deep(element))
        else:
            result.append(element)
    return result

# Test with an irregular, deeply nested list
deep_list = [1, [2, [3, 4], 5], [6, [7, [8, 9]]]]
print(flatten_deep(deep_list))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Notice that we used the extend() method here to merge the returned list into our main result. Once your data is completely flat, it becomes much easier to find the index of an item in a list in Python or sort your values.

Common Errors When Flattening Lists

Beginners often run into a few common issues when trying to flatten lists. Knowing these mistakes will save you debugging time.

  • Forgetting the unpacking operator: Passing itertools.chain(my_list) without the * symbol will not flatten the sublists.
  • Using shallow methods on deep lists: List comprehension will leave inner lists intact if your data has three or more levels of nesting.
  • Flattening strings by mistake: Strings are iterable in Python. Be careful not to accidentally split words into individual characters when using generic flattening tools.

Which Method Should You Choose?

Choosing the right approach depends on your data structure and performance needs. Keep these simple rules in mind.

  • Use list comprehension for everyday 2D lists where readability and clean code matter most.
  • Use itertools.chain() when processing large 2D lists where speed is critical.
  • Use recursion whenever your list has unknown or uneven nesting depths.

Conclusion

Learning how to flatten a nested list is an essential skill for every Python programmer. Simple 2D lists are easy to handle with list comprehensions or itertools.chain(). Complex, multi-level lists require a recursive function with type checking.

Practice these examples in your code editor to see how each method behaves. Once you master list flattening, handling messy real-world data becomes much simpler.