Skip to content

Flatten List in Python: Effortlessly Combine Nested Lists

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’ve successfully flattened a list of lists using a for loop and the .extend() method.

Using a Comprehension to Flatten a List of Lists

Another concise way to flatten a list of lists in Python is by using a list comprehension. A list comprehension is a compact way to create lists based on existing lists or other iterables. In this case, you can use a nested list comprehension to iterate over each sublist and each item within the sublist, and then combine all the items into a one-dimensional list.

Here’s the code to achieve this:

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

The list comprehension creates a new list by iterating over each sublist in the matrix and then iterating over each item within the sublist. The items are added to the new list using the item variable.

You can test this approach by running the following code:

result = flatten_comprehension(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]

Using a list comprehension provides a concise and readable way to flatten a list of lists in Python.

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

Python also provides several built-in tools and functions in the standard library that can be used to flatten a list. Here are three commonly used tools: itertools.chain(), functools.reduce(), and sum().

Chaining Iterables With itertools.chain()

The itertools.chain() function allows you to chain multiple iterables together into a single sequence. You can use it to chain together all the sublists in your matrix and obtain a flattened list. Here’s how you can write the code:

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

In this code, the itertools.chain() function is called with the star (*) operator, which unpacks the sublists from the matrix and passes them as separate arguments to chain(). The result is a chained sequence of all the items from the sublists, which is then converted to a list using the list() function.

You can test this approach by running 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]

Concatenating Lists With functools.reduce()

The functools.reduce() function can be used to recursively concatenate multiple lists into a single list. Here’s how you can write the code:

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

In this code, the reduce() function is called with a lambda function as the first argument. The lambda function takes two arguments (x and y) and performs the concatenation operation by adding them together. The reduce() function applies the lambda function sequentially to all the sublists in the matrix, combining them into a single list.

You can test this approach by running 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]

Using sum() to Concatenate Lists

The sum() function can also be leveraged to concatenate lists together. You can use it in combination with a list comprehension to flatten the matrix into a single list. Here’s how you can write the code:

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

In this code, the sum() function is called with two arguments: the matrix and an empty list. When sum() is called with a list as the first argument, it performs concatenation by adding all the elements together. By passing an empty list as the second argument, you ensure that the concatenation is performed with an empty list as the starting value.

You can test this approach by running 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]

Using standard-library tools and built-in functions like itertools.chain(), functools.reduce(), and sum() can provide alternative ways to flatten a list in Python.

Considering Performance While Flattening Your Lists

When working with large datasets or performance-critical scenarios, the choice of method for flattening a list can impact the efficiency of your code. The iterative approaches using a for loop, list comprehension, itertools.chain(), functools.reduce(), and sum() have different performance characteristics.

For small to medium-sized lists, the performance differences between these methods are usually negligible. However, if you’re dealing with large lists or performing computations involving nested lists frequently, it’s worth considering the performance implications.

In general, the iterative approaches using a for loop or list comprehension tend to be faster than using itertools.chain() or functools.reduce(). sum() can be an effective and concise method, but it may have performance limitations for very large lists.

In some scenarios, using specialized libraries like NumPy can provide significant performance improvements, especially in the context of data science operations.

Flattening Python Lists for Data Science With NumPy

NumPy is a popular library for numerical computing in Python. It provides a powerful flatten() function that can efficiently flatten multidimensional arrays, including lists of lists. Here’s how you can use NumPy to flatten your matrix:

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

In this code, the flatten() function from NumPy is applied directly to the matrix. The resulting flattened array is returned as the output.

To use this approach, you’ll need to have NumPy installed. If you haven’t done so yet, you can install it using the following command:

pip install numpy

You can test this approach by running 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’s flatten() function is optimized for performance and can handle large multidimensional arrays efficiently. If you’re working with data science operations that involve flattened lists, NumPy can be a valuable tool.

Conclusion

Flattening a list of lists is a common operation when working with hierarchical or multidimensional data in Python. This tutorial has shown you several ways to flatten a list using different approaches, including a for loop, list comprehension, and various standard-library and built-in tools like itertools.chain(), functools.reduce(), sum(), and NumPy.

Understanding the different ways to flatten a list and considering the performance implications can help you choose the best approach for your specific use case. By effectively flattening your lists, you can manipulate your data more easily and perform computations more efficiently.