Skip to content

Flatten List in Python: Simplify Nested Structures

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, let’s consider 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 let’s say that you want to turn this matrix into a one-dimensional list:

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

How do you flatten the matrix and get a one-dimensional list like the one mentioned above? In this tutorial, we will explore different methods to accomplish this task.

How to Flatten a List of Lists With a for Loop

One way to flatten a list of lists is by using a for loop. Here are the steps you can follow:

  1. Create an empty list to store the flattened data.
  2. Iterate over each nested list 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.

To implement this solution, we can define a function called flatten_extend:

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

Inside the flatten_extend function, a new empty list called flat_list is created. This list will store the flattened data when it is extracted from the matrix. Then, a loop is started to iterate over the inner lists from the matrix. In each iteration, the content of the current sublist is added to flat_list using the .extend() method.

Let’s run the following code to see the flatten_extend function in action:

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

The output will be [9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5].

Using a for loop and the .extend() method is a straightforward and readable way to flatten a list of lists in Python.

Using a Comprehension to Flatten a List of Lists

Another concise way to flatten a list of lists is by using a list comprehension. Here’s how you can do it:

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

In this solution, a list comprehension is used to iterate over each sublist in the matrix and then iterate over each item in the sublist. The resulting items are collected in a new list.

Let’s test the flatten_comprehension function with the same matrix:

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

Again, the output will be [9, 3, 8, 3, 4, 5, 2, 8, 6, 4, 3, 1, 1, 0, 4, 5].

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

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

Python’s standard library and built-in functions also offer tools to flatten a list of lists. Here are some options:

Chaining Iterables With itertools.chain()

The itertools.chain() function can be used to chain multiple iterables together. By passing the nested lists as arguments to itertools.chain(), you can create a single iterator that produces all the items from the nested lists. You can then convert this iterator into a list.

Here’s an example:

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

In this solution, *matrix is used to unpack the nested lists as separate arguments for itertools.chain(). The resulting iterator is then converted into a list using the list() function.

Concatenating Lists With functools.reduce()

The functools.reduce() function can be used to apply a function of two arguments cumulatively to the items of a sequence. By passing the operator.add function as the first argument and the nested lists as the second argument to functools.reduce(), you can concatenate the lists element by element. Again, you can convert the resulting iterable into a list.

Here’s an example:

import functools
import operator
def flatten_reduce(matrix):
return list(functools.reduce(operator.add, matrix))

In this solution, operator.add is used as the function to be applied cumulatively, and matrix is passed as the iterable to functools.reduce().

Using sum() to Concatenate Lists

Python’s sum() function can be used to sum the elements of a list of lists when the elements are numeric values. By providing an empty list as the second argument to sum(), you can start the sum operation with an empty list. This causes sum() to concatenate the nested lists.

Here’s an example:

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

In this solution, [] is specified as the second argument to sum() to start the sum operation with an empty list.

Considering Performance While Flattening Your Lists

When flattening large lists of lists, the performance of your solution may become a concern. Some methods discussed earlier may have different performance characteristics.

For example, the flatten_extend() function using a for loop and the .extend() method has a linear time complexity, making it efficient for most cases. On the other hand, using a list comprehension or the itertools.chain() approach may have a similar time complexity but can be slightly slower due to additional overhead.

It’s important to consider the specific requirements of your project and choose the method that best suits your needs.

Flattening Python Lists for Data Science With NumPy

If you’re working on data science projects, you can also flatten lists using the NumPy library. NumPy provides various functions to manipulate arrays, including flattening.

Here’s an example of how to flatten a list of lists using NumPy:

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

In this solution, the np.array() function is used to convert the nested lists into a NumPy array. Then, the .flatten() method is applied to the array to obtain a one-dimensional representation. Finally, the .tolist() method is used to convert the NumPy array back into a Python list.

Conclusion

Flattening a list of lists is a common operation when working with nested data structures in Python. In this article, we explored different methods to flatten a list of lists, including using a for loop, a list comprehension, standard-library and built-in tools, and the NumPy library for data science applications. Each method has its advantages and considerations in terms of readability, efficiency, and performance. By understanding these methods, you can choose the most appropriate approach for your specific project requirements.