Flatten List in Python

What is List Flattening?

Flattening a list means converting a nested list (a list containing other lists as elements) into a single, one-dimensional list. The goal is to remove all levels of nesting so that we are left with a flat list of individual elements.

For example:

Input (Nested List):

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

Output (Flattened List):

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

This transformation is useful in many cases, such as:

  • Processing complex data structures (like JSON, XML).
  • Cleaning and preparing data for machine learning.
  • Handling nested matrices or arrays.

Methods to Flatten a List in Python

There are multiple ways to flatten a list, each with its own use cases, advantages, and drawbacks.

1. Using a For Loop (Basic Approach)

The most intuitive way to flatten a list is by using a for loop.

def flatten_list(nested_list):
    flat_list = []
    for sublist in nested_list:  # Iterate through each sublist
        for item in sublist:  # Iterate through each item in the sublist
            flat_list.append(item)  # Add the item to the new flat list
    return flat_list

nested = [[1, 2, 3], [4, 5], [6, 7, 8]]
print(flatten_list(nested))

How it Works?

  1. Creates an empty list (flat_list).
  2. Loops through each sublist in nested_list.
  3. Loops through each item in the sublist and appends it to flat_list.
  4. Returns the flattened list.

Output:

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

Advantages:

  • Clear and easy to understand.
  • Does not require importing additional modules.

Disadvantages:

  • Only applies to a 2D list, which has only one nesting level.
  • Inefficient for large lists as it uses multiple loops.

2. Using List Comprehension (Pythonic Approach)

Python’s list comprehension provides a cleaner and faster way to flatten a list.

nested = [[1, 2, 3], [4, 5], [6, 7, 8]]
flat_list = [item for sublist in nested for item in sublist]
print(flat_list)

How it Works?

  • The first loop, for sublist in nested iterates over each sublist.
  • The second loop for item in sublist iterates through each element inside the sublist.
  • Each item is added directly to flat_list.

Output:

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

Advantages:

  • More readable and compact than a for-loop.
  • Faster than the basic loop approach.

3. Using itertools.chain() (Efficient Approach)

Python’s itertools.chain() function is optimized for flattening shallow nested lists.

import itertools

nested = [[1, 2, 3], [4, 5], [6, 7, 8]]
flat_list = list(itertools.chain(*nested))  # Unpack and merge lists
print(flat_list)How it Works?

How it Works?

  • *nested unpacks the list so that each sublist is passed as a separate argument to itertools.chain().
  • itertools.chain() merges the elements into a single iterable.
  • list() changes the iterable to a Python list.

Output:

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

Advantages:

  • Highly efficient for flattening 2D lists.
  • Uses optimized C-based iteration, making it faster than loops.

Disadvantages:

  • Only works for 2D lists.
  • Does not handle deeply nested lists.

4. Using Recursion (Handles Deeply Nested Lists)

For deeply nested lists, recursion is the best approach.

def flatten_recursive(nested_list):
    flat_list = []
    for item in nested_list:
        if isinstance(item, list):  # If item is a list, recursively flatten it
            flat_list.extend(flatten_recursive(item))
        else:
            flat_list.append(item)  # If item is not a list, add to the flat list
    return flat_list

nested = [[1, [2, 3]], [4, [5, [6, 7], 8]]]
print(flatten_recursive(nested))

How it Works?

  1. Loops through each element in nested_list.
  2. If an element is a list, calls itself recursively to flatten it.
  3. If it is not a list, add it to flat_list.
  4. This process continues until the entire structure is flattened.

Output:

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

Advantages:

  • Works for any depth of nesting.

Disadvantages:

  • Slower for large lists (due to recursive function calls).
  • Can cause recursion depth errors for deeply nested lists.

5. Using sum() with an Empty List

A trick using Python’s sum() function.

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

How it Works?

  • sum() is used to add up elements of lists.
  • [] is given as the starting value, ensuring the result is a list.

Output:

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

Disadvantages:

  • Not recommended due to performance issues.
  • Does not work for deeply nested lists.

6. Using NumPy (Best for Numerical Data)

If working with numerical data, NumPy provides an easy way to flatten.

import numpy as np

nested = [[1, 2, 3], [4, 5], [6, 7, 8]]
flat_list = np.array(nested).flatten().tolist()
print(flat_list)

How it Works?

  1. np.array(nested) converts the list into a NumPy array.
  2. .flatten() flattens the array.
  3. .tolist() converts it back to a Python list.

Output:

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

Advantages:

  • Highly optimized for large numerical lists.
  • Fastest method for numerical operations.

Disadvantages:

  • Requires NumPy.
  • Not ideal for non-numeric data.

Comparison of Methods

MethodHandles Deep NestingPerformanceReadability
For LoopNoModerateGood
List ComprehensionNoFastBest
itertools.chain()NoVery FastExcellent
RecursionYesSlowerMedium
sum(nested, [])NoFast (but not recommended)Poor
NumPy .flatten()NoFastGood (for numbers)

Final Thoughts

  • For one-level lists, use list comprehension or itertools.chain().
  • For deeply nested lists, use recursion.
  • For numerical data, use NumPy.
  • Do not use sum(nested, []) for the sake of performance.