Skip to content

Python Flatten List - Easily Convert Nested Lists into a Flat List

CodeMDD.io

How to Flatten a List of Lists in Python

Sometimes, when you’re working with data, you may have the data as a list of nested lists. A common operation is to flatten this data into a one-dimensional list in Python. Flattening a list involves converting a multidimensional list, such as a matrix, into a one-dimensional list.

To better illustrate what it means to flatten a list, say that you have the following matrix of numeric values:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]

The matrix variable holds a Python list that contains four nested lists. Each nested list represents a row in the matrix. The rows store four items or numbers each. Now say that you want to turn this matrix into the following list:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

How do you manage to flatten your matrix and get a one-dimensional list like the one above? In this tutorial, you’ll learn how to do that in Python.

How to Flatten a List of Lists With a for Loop

How can you flatten a list of lists in Python? In general, to flatten a list of lists, you can run the following steps either explicitly or implicitly:

  1. Create a new empty list to store the flattened data.
  2. Iterate over each nested list or sublist in the original list.
  3. Add every item from the current sublist to the list of flattened data.
  4. Return the resulting list with the flattened data.

You can follow several paths and use multiple tools to run these steps in Python. Arguably, the most natural and readable way to do this is to use a for loop, which allows you to explicitly iterate over the sublists.

Then you need a way to add items to the new flattened list. For that, you have a couple of valid options. First, you’ll turn to the .extend() method from the list class itself, and then you’ll give the augmented concatenation operator (+=) a go.

To continue with the matrix example, here’s how you would translate these steps into Python code using a for loop and the .extend() method:

def flatten_extend(matrix):
flat_list = []
for row in matrix:
flat_list.extend(row)
return flat_list

Inside flatten_extend(), you first create a new empty list called flat_list. You’ll use this list to store the flattened data when you extract it from matrix. Then you start a loop to iterate over the inner, or nested, lists from matrix. In this example, you use the name row to represent the current nested list.

In every iteration, you use .extend() to add the content of the current sublist to flat_list. This method takes an iterable as an argument and appends its items to the end of the target list.

Now go ahead and run the following code to check that your function does the job:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
result = flatten_extend(matrix)
print(result)

The output should be:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

Congratulations! You have successfully flattened the list of lists using a for loop and the .extend() method.

Using a Comprehension to Flatten a List of Lists

Another approach to flatten a list of lists is to use a comprehension. Comprehensions provide a concise way to create and manipulate lists in Python.

To flatten a list using a comprehension, you can use nested comprehensions. The outer comprehension iterates over the original list, while the inner comprehension iterates over the sublists and flattens them into a single list. Here’s an example using the matrix list:

def flatten_comprehension(matrix):
return [item for sublist in matrix for item in sublist]

In this function, the comprehension has two parts:

  • item for sublist in matrix: This is the inner comprehension that iterates over each sublist in the matrix list. It returns each individual item from the sublist.
  • for sublist in matrix: This is the outer comprehension that iterates over the matrix list. It runs the inner comprehension for each sublist.

To test the flatten_comprehension() function, you can use the same matrix list as before and run the following code:

result = flatten_comprehension(matrix)
print(result)

The output should be:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

Once again, you have successfully flattened the list of lists, this time using a comprehension.

Flattening a List Using Standard-Library and Built-in Tools

Python provides several built-in tools and modules in its standard library that can help you flatten a list of lists. Let’s explore three of them: itertools.chain(), functools.reduce(), and sum().

Chaining Iterables With itertools.chain()

itertools.chain() is a function that takes multiple iterables as arguments and returns an iterator that produces all the items from each iterable consecutively. This can be used to chain together the sublists of a list of lists and flatten them.

You’ll need to import itertools to use the chain() function. Here’s an example of how to flatten a list using itertools.chain():

import itertools
def flatten_chain(matrix):
return list(itertools.chain(*matrix))

In this function, you use the * operator to unpack the matrix list and pass its sublists as separate arguments to chain(). This ensures that chain() receives each sublist individually.

To test the flatten_chain() function, you can use the same matrix list as before and run the following code:

result = flatten_chain(matrix)
print(result)

The output should be the same as before:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

Once again, you have successfully flattened the list of lists, this time using itertools.chain().

Concatenating Lists With functools.reduce()

functools.reduce() is a higher-order function that applies a binary function (taking two arguments) to the items of a sequence from left to right. In the context of flattening a list of lists, you can use reduce() to concatenate the sublists into a single list.

You’ll need to import functools to use the reduce() function. Here’s an example of how to flatten a list using functools.reduce():

import functools
def flatten_reduce(matrix):
return functools.reduce(lambda x, y: x + y, matrix)

In this function, you pass a lambda function as the first argument to reduce(). This lambda function takes two arguments, x and y, and concatenates them together using the + operator.

To test the flatten_reduce() function, you can use the same matrix list as before and run the following code:

result = flatten_reduce(matrix)
print(result)

The output should be the same as before:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

Once again, you have successfully flattened the list of lists, this time using functools.reduce().

Using sum() to Concatenate Lists

In Python, the built-in sum() function can be used to sum the elements of an iterable. However, it can also be used to concatenate lists by providing an empty list as the initial argument to sum().

Here’s an example of how to flatten a list using sum():

def flatten_sum(matrix):
return sum(matrix, [])

In this function, you pass an empty list ([]) as the initial argument to sum(). This ensures that sum() concatenates the sublists of the matrix list into a single list.

To test the flatten_sum() function, you can use the same matrix list as before and run the following code:

result = flatten_sum(matrix)
print(result)

The output should be the same as before:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

Once again, you have successfully flattened the list of lists, this time using sum().

Considering Performance While Flattening Your Lists

When flattening large lists of lists, performance becomes an important consideration. Each method discussed so far has its own performance characteristics, and the most efficient approach may depend on the size and structure of your data.

To compare the performance of the methods, you can use the timeit module. Here’s an example of how to measure the execution time of each method using a large matrix:

import timeit
large_matrix = [[1] * 1000 for _ in range(1000)]
def time_method(method):
return timeit.timeit(lambda: method(large_matrix), number=100)
def measure_performance():
methods = {
"For Loop and extend()": flatten_extend,
"Comprehension": flatten_comprehension,
"itertools.chain()": flatten_chain,
"functools.reduce()": flatten_reduce,
"sum()": flatten_sum,
}
for name, method in methods.items():
duration = time_method(method)
print(f"{name}: {duration} seconds")
measure_performance()

Running this code will output the execution time of each method in seconds:

For Loop and extend(): 0.5824634050000104 seconds
Comprehension: 0.4199089239999935 seconds
itertools.chain(): 1.6843475150000062 seconds
functools.reduce(): 7.178833248000037 seconds
sum(): 8.598935226000038 seconds

From these results, you can see that using a comprehension is generally faster than using the other methods. However, the performance may vary depending on the size and structure of your data. It’s important to consider these factors when flattening large lists of lists.

Flattening Python Lists for Data Science With NumPy

If you’re working with data science applications, it’s worth mentioning that the NumPy library provides a convenient way to flatten multi-dimensional arrays and lists.

To flatten a list of lists using NumPy, you can use the numpy.flatten() or numpy.ravel() functions. Here’s an example using numpy.flatten():

import numpy as np
def flatten_numpy(matrix):
return np.array(matrix).flatten().tolist()

In this function, you first convert the matrix list into a NumPy array using np.array(). Then you apply the .flatten() method to the array to flatten it. Finally, you use .tolist() to convert the NumPy array back into a regular Python list.

To test the flatten_numpy() function, you can use the same matrix list as before and run the following code:

result = flatten_numpy(matrix)
print(result)

The output should be the same as before:

[9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5]

NumPy provides additional functionality for working with multi-dimensional arrays, so if you’re working extensively with data science, it may be worth exploring its capabilities further.

Conclusion

Flattening a list of lists in Python is a common operation that involves converting a nested structure into a one-dimensional list. In this tutorial, you learned several methods to accomplish this task, including using a for loop, comprehensions, standard-library tools like itertools.chain() and functools.reduce(), and built-in functions like sum(). You also learned about the performance considerations when working with large data sets and how to flatten lists using NumPy for data science applications.

Now you have a solid understanding of how to flatten a list of lists in Python, allowing you to work more effectively with nested data structures and extract the information you need.

CodeMDD.io