Skip to content

Effortlessly Flatten Lists in Python

CodeMDD.io

How to Flatten a List of Lists in Python

Table of Contents

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)

When you run the code, you should see the following output:

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

The flatten_extend() function successfully flattens the matrix into a one-dimensional list.

Using a Comprehension to Flatten a List of Lists

Another concise and expressive way to flatten a list of lists in Python is to use a list comprehension. A list comprehension allows you to create a new list by iterating over an existing iterable and applying a transformation or filter to each element.

To flatten a list of lists using a comprehension, you can use a nested comprehension. The outer comprehension iterates over each nested list, while the inner comprehension extracts the items from each nested list and combines them into one flat list. Here’s how you can implement it:

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

In this implementation, the outer comprehension iterates over matrix, and the inner comprehension iterates over each sublist. The item variable represents an item from a nested list, and it’s added to the resulting list.

You can test this implementation by running the following code:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
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]

The flatten_comprehension() function also successfully flattens the matrix into a one-dimensional list using a list comprehension.

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

In addition to using a for loop and a list comprehension, you can leverage some tools provided by the Python standard library and built-in functions to flatten a list of lists.

Chaining Iterables With itertools.chain()

The itertools module in the Python standard library provides a function called chain() that allows you to concatenate multiple iterables into one. You can use chain() to flatten a list of lists by passing each sublist as an argument. Here’s an example:

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

To use this implementation, you need to unpack matrix using the * operator and pass it as arguments to the chain() function. The resulting iterator is then converted into a list using the list() constructor.

You can test this implementation by running the following code:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
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]

The flatten_chain() function successfully flattens the matrix using the chain() function from the itertools module.

Concatenating Lists With functools.reduce()

Another option is to use the reduce() function from the functools module in the Python standard library. reduce() allows you to apply a function of two arguments cumulatively to the elements of an iterable, reducing it to a single value. In the case of flattening a list of lists, you can use the + operator to concatenate the sublists. Here’s an example:

import functools
def flatten_reduce(matrix):
return functools.reduce(lambda a, b: a + b, matrix)

In this implementation, the reduce() function takes a lambda function as its first argument. The lambda function takes two arguments, a and b, and combines them using the + concatenation operator.

You can test this implementation by running the following code:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
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]

The flatten_reduce() function successfully flattens the matrix using the reduce() function from the functools module.

Using sum() to Concatenate Lists

In addition to chain() and reduce(), you can use the built-in sum() function to concatenate lists. The sum() function takes an iterable and an optional start value and returns the sum of the items in the iterable. When you pass a list as the iterable, sum() concatenates the lists. Here’s an example:

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

In this implementation, sum() takes matrix as the iterable and [] as the start value, effectively concatenating the sublists into a single list.

You can test this implementation by running the following code:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
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]

The flatten_sum() function successfully flattens the matrix using the sum() function.

Considering Performance While Flattening Your Lists

When working with large datasets or performance-critical applications, it’s important to consider the performance implications of different methods to flatten lists.

Due to the overhead of creating new lists and iterating over items, some methods like list comprehensions can be slower than others. For example, using itertools.chain() or the + operator can be more efficient in terms of memory usage and speed.

It’s also worth mentioning that the performance of flattening lists can vary depending on the size and structure of your data. Therefore, it’s recommended to benchmark and compare different approaches in your specific use case to make an informed decision.

Flattening Python Lists for Data Science With NumPy

If you’re working with data science tasks in Python, the NumPy library provides efficient ways to flatten multidimensional arrays, including lists of lists. NumPy arrays are a popular data structure for numerical computations, and they offer performance benefits over regular Python lists.

To flatten a list of lists using NumPy, you can convert it to a NumPy array and then use the flatten() method. Here’s an example:

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

In this implementation, you first convert matrix to a NumPy array using np.array(). Then you call the flatten() method on the array, which returns a one-dimensional flattened array. Finally, you convert the array back to a regular Python list using tolist().

You can test this implementation by running the following code:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
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]

The flatten_numpy() function successfully flattens the matrix using NumPy.

Conclusion

Flattening a list of lists in Python can be accomplished in different ways, depending on your requirements and preferences. You can use a for loop, a list comprehension, or leverage standard-library and built-in tools like itertools.chain(), reduce(), and sum(). Additionally, if you’re working with data science tasks, NumPy provides efficient methods to flatten multidimensional arrays.

When choosing a method, consider the performance implications and also take into account the structure and size of your data. Benchmark and compare different approaches to find the most suitable one for your specific use case.

CodeMDD.io