Skip to content

Effortlessly Flatten List of Lists in Python

[

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],
]
flattened_matrix = flatten_extend(matrix)
print(flattened_matrix)

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

Using a Comprehension to Flatten a List of Lists

Another common and concise way to flatten a list of lists is by using a list comprehension. List comprehensions allow you to create new lists by iterating over an existing list and applying transformations or conditions.

To flatten a list of lists with a list comprehension, you’ll use a nested loop structure. The outer loop iterates over each sublist, and the inner loop iterates over each item in the current sublist. Inside the list comprehension, you can simply add the items to a new list.

Here’s an example of a list comprehension to flatten the matrix:

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

In this code, flat_list is created using a list comprehension. The expression item for sublist in matrix for item in sublist iterates over each sublist in matrix and each item in each sublist, adding the items to flat_list.

To test this function, run the following code:

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

You should see the same flattened list as before:

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

This time, you achieved the same result using a list comprehension.

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

Python provides several tools and functions in its standard library and built-in functions that can help you flatten lists. Let’s explore three common approaches: chaining iterables with itertools.chain(), concatenating lists with functools.reduce(), and using sum() to concatenate lists.

Chaining Iterables With itertools.chain()

The itertools.chain() function allows you to chain together multiple iterables into a single iterable. By passing the iterables as arguments to itertools.chain(), you can concatenate them and iterate over the concatenated result.

In the case of flattening lists, you can pass the nested lists directly to itertools.chain() to create an iterable of all the items in the nested lists. Then you can convert this iterable to a list using list().

Here’s an example:

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

In this code, itertools.chain(*matrix) unpacks the nested lists and passes them as separate arguments to itertools.chain(). This creates a chain of iterables where each iterable is one of the nested lists. The resulting chained iterable is then converted into a list using list().

To test this function, run the following code:

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

You should see the same flattened list:

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

This time, you used itertools.chain() to flatten the list.

Concatenating Lists With functools.reduce()

The functools.reduce() function allows you to apply a function of two arguments cumulatively to the items of a list. By passing a concatenation function to functools.reduce(), you can concatenate all the nested lists together into a single list.

Here’s an example:

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

In this code, functools.reduce(lambda x, y: x + y, matrix) applies the lambda function lambda x, y: x + y to each pair of items in matrix, effectively concatenating them together.

To test this function, run the following code:

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

You should see the same flattened list:

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

This time, you used functools.reduce() to flatten the list.

Using sum() to Concatenate Lists

The sum() function in Python is typically used to sum numbers. However, it can also be used to concatenate lists by providing an empty list as the initial value and adding the nested lists as the iterable arguments.

Here’s an example:

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

In this code, sum(matrix, []) takes matrix as the iterable to concatenate and [] as the initial value. The sum() function then performs the concatenation operation.

To test this function, run the following code:

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

You should see the same flattened list:

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

This time, you used sum() to flatten the list.

Considering Performance While Flattening Your Lists

When working with large lists or nested structures, performance becomes important. The methods we’ve explored so far are suitable for most cases, but they may not be the most efficient for very large lists. In those cases, other approaches, such as using the yield from statement or using an external library like NumPy, can improve performance.

Flattening Python Lists for Data Science With NumPy

If you’re specifically working with data science or numerical computing, the NumPy library provides efficient functions for working with multidimensional arrays and matrices. NumPy’s flatten() function can be used to flatten a list of lists into a one-dimensional array.

Here’s an example:

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

In this code, np.array(matrix).flatten() converts matrix into a NumPy array and then uses the flatten() function to flatten it. The resulting flattened array is then converted back to a Python list using .tolist().

To test this function, run the following code:

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

You should see the same flattened list:

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

This time, you used NumPy to flatten the list, which can be more efficient for large matrices or arrays.

Conclusion

Flattening a list of lists in Python is a common operation when working with nested data structures. In this tutorial, you learned several ways to flatten lists using different techniques and functions. You explored using a for loop and the .extend() method, a list comprehension, standard-library tools like itertools.chain() and functools.reduce(), built-in functions like sum(), and NumPy’s flatten() function for data science.

Remember to consider performance when working with large lists or matrices, and choose the most suitable approach based on your specific requirements.