Skip to content

Python Tutorial: Effortlessly Flatten 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:

Python

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:

Python

[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:

Python

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:

Python

flattened = flatten_extend(matrix)
print(flattened)

Output

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

As you can see, flatten_extend() successfully flattens the matrix into a one-dimensional list.

Using a Comprehension to Flatten a List of Lists

Another concise and readable way to flatten a list of lists is by using a list comprehension. With a list comprehension, you can achieve the same result as the previous example in a single line of code.

Here’s how you can flatten a list using a list comprehension:

Python

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

The above code is equivalent to the previous code using a for loop and .extend(). It creates a new list by iterating over each item in each sublist (item for row in matrix) and adding it to the new list. The resulting list is automatically returned from the function.

You can test this method by running the following code:

Python

flattened = flatten_comprehension(matrix)
print(flattened)

Output

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

The output should match the previous output, indicating that the list comprehension successfully flattened the matrix.

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

Python’s standard library and built-in functions provide additional tools that you can use to flatten a list of lists. In this section, you’ll explore three different methods: chaining iterables with itertools.chain(), concatenating lists with functools.reduce(), and using sum() to concatenate lists.

Chaining Iterables With itertools.chain()

The itertools module in Python’s standard library provides various functions for creating and manipulating iterators and iterables. One of these functions, chain(), can be used to chain together multiple iterables into a single iterator.

To flatten a list of lists using itertools.chain(), you need to pass each sublist as a separate argument to the function. Here’s an example that shows how to use chain() to flatten a list of lists:

Python

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

In the code above, the *matrix syntax is used to unpack the matrix list and pass each sublist as a separate argument to itertools.chain(). The resulting iterator is then converted back into a list using the list() function.

You can test this method by running the following code:

Python

flattened = flatten_chain(matrix)
print(flattened)

Output

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

The output should once again match the previous output, indicating that flatten_chain() successfully flattened the matrix.

Concatenating Lists With functools.reduce()

The functools module in Python’s standard library provides tools for working with higher-order functions, such as reduce(). The reduce() function takes a two-argument function and an iterable as arguments and applies the function cumulatively to the items of the iterable, from left to right, so as to reduce the iterable to a single value.

To flatten a list of lists using functools.reduce(), you can use the built-in list function as the two-argument function and pass the flattened list as the iterable.

Here’s an example:

Python

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

In the code above, list.__add__ represents the built-in + operator for lists. The reduce() function applies the + operator cumulatively to the items of matrix, resulting in a single list.

You can test this method by running the following code:

Python

flattened = flatten_reduce(matrix)
print(flattened)

Output

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

Once again, the output should match the previous output, indicating that flatten_reduce() successfully flattened the matrix.

Using sum() to Concatenate Lists

The sum() function in Python is typically 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 value.

To flatten a list of lists using sum(), you need to pass the list of lists to sum() along with an empty list as the initial value.

Here’s an example:

Python

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

In the code above, sum(matrix, []) concatenates the lists in matrix using the + operator and an empty list as the initial value.

You can test this method by running the following code:

Python

flattened = flatten_sum(matrix)
print(flattened)

Output

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

Once again, the output should match the previous output, indicating that flatten_sum() successfully flattened the matrix.

Considering Performance While Flattening Your Lists

When dealing with large lists or matrices, the performance of your code becomes crucial. In this section, you’ll explore the performance implications of the different methods covered so far and learn how to optimize your code for better performance.

One important factor to consider is the time complexity of each method. The flatten_extend() and flatten_comprehension() methods both have a time complexity of O(n), where n is the total number of elements in the matrix. This is because each item in the matrix needs to be visited and added to the new list once.

On the other hand, the flatten_chain(), flatten_reduce(), and flatten_sum() methods have a time complexity of O(n), where n is the total number of elements in the matrix. This is because each item in the matrix is processed once, either by itertools.chain(), functools.reduce(), or sum().

It’s worth noting that the flatten_extend() method is slightly faster than the other methods when dealing with larger matrices. This is because the .extend() method is highly optimized in CPython, the most widely used implementation of Python.

However, the performance difference between the different methods is generally negligible unless you’re working with extremely large matrices or have strict performance requirements.

Flattening Python Lists for Data Science With NumPy

If you’re working with data science in Python, chances are you’re already familiar with NumPy. NumPy is a powerful library for numerical computing in Python, providing support for large, multi-dimensional arrays and matrices.

NumPy provides a straightforward way to flatten a list of lists using its flatten() function. Here’s an example:

Python

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

In the code above, np.array(matrix) converts the matrix into a NumPy array, and .flatten() flattens the array into a one-dimensional array. Finally, .tolist() converts the NumPy array back into a standard Python list.

You can test this method by running the following code:

Python

flattened = flatten_numpy(matrix)
print(flattened)

Output

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

As expected, the output matches the previous output, indicating that flatten_numpy() successfully flattened the matrix using NumPy.

Conclusion

Flattening a list of lists is a common operation when working with multidimensional data in Python. In this tutorial, you learned several methods to flatten a list of lists, including using a for loop, a list comprehension, standard-library tools, and NumPy.

You also explored the performance implications of each method and discovered that the choice of method generally has a minimal impact on performance unless you’re dealing with extremely large matrices.

Next time you need to flatten a list of lists in Python, you’ll have several effective methods at your disposal. Choose the one that best fits your needs and enjoy the flexibility and power of Python in handling data.