Skip to content

Effortlessly Flatten Python List

[

How to Flatten a List of Lists in Python

by [Your Name]

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, 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 the goal is to turn this matrix into a one-dimensional list like this:

[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

To flatten a list of lists in Python, you can follow these steps:

  1. Create a new 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.

One way to achieve this is by using a for loop to iterate over the sublists and the .extend() method to add the items to the new flattened list.

Here’s an example 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 the flatten_extend() function, an empty list called flat_list is created to store the flattened data. Then, a loop is used to iterate over the inner lists from the matrix. In each iteration, the .extend() method is used to add the items of the current sublist to flat_list.

To check if the function works, you can run the following code:

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 will be:

[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 way to flatten a list of lists is by using a comprehension.

Python comprehensions allow you to create new lists or other objects based on existing sequences. In this case, you can use a nested comprehension to iterate over the sublists and flatten them.

Here’s an example:

matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
flattened = [item for sublist in matrix for item in sublist]
print(flattened)

The output will be the same as before:

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

In this example, the comprehension [item for sublist in matrix for item in sublist] is used to iterate over each sublist in the matrix and add every item to the resulting list. The items are added in the order they appear in the sublists.

Using a comprehension can be a more concise way to flatten lists, especially when working with smaller datasets.

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

Python provides several tools in its standard library and built-in functions that can help you flatten lists more efficiently than using a for loop or a comprehension.

Chaining Iterables With itertools.chain()

The itertools.chain() function allows you to combine multiple iterables into a single one. You can use it to chain the sublists of a list of lists and create an iterator that yields the flattened data.

Here’s an example:

import itertools
matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
flattened = list(itertools.chain(*matrix))
print(flattened)

The output will be the same:

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

In this example, the itertools.chain(*matrix) expression is used to chain the sublists of the matrix. The *matrix syntax unpacks the elements of the matrix, providing them as separate arguments to itertools.chain(). Applying the list() function to the result creates a list from the iterator yielded by itertools.chain().

Using itertools.chain() can create a more memory-efficient solution when dealing with large lists of lists, as it does not store the flattened list in memory all at once.

Concatenating Lists With functools.reduce()

The functools.reduce() function allows you to apply a particular operation to the elements of an iterable, reducing them to a single value. You can use it to concatenate the sublists of a list of lists and create a flattened list.

Here’s an example:

import functools
matrix = [
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
]
flattened = functools.reduce(lambda x, y: x + y, matrix)
print(flattened)

The output will be the same:

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

In this example, the functools.reduce(lambda x, y: x + y, matrix) expression is used to concatenate the sublists of the matrix. The lambda function (lambda x, y: x + y) takes two arguments x and y and combines them using the + operator. functools.reduce() applies this lambda function to all pairs of sublists in the matrix, reducing them to a single list.

Using functools.reduce() can be more efficient than a for loop or a comprehension when flattening large lists of lists.

Using sum() to Concatenate Lists

Another built-in function that can be used to flatten a list of lists is sum(). By providing an empty list as the starting value and using the + operator, you can concatenate the sublists into a single flattened list.

Here’s an example:

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

The output will be the same:

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

In this example, sum(matrix, []) concatenates the sublists of the matrix using the + operator. The empty list [] serves as the starting value to which the sublists are added.

Using sum() can provide a concise solution for flattening small lists of lists. However, for large lists, it may not be as memory-efficient as other methods, as it creates new objects in each addition.

Considering Performance While Flattening Your Lists

When flattening lists, it’s important to consider the performance implications of each method. The time and memory efficiency can vary depending on the size of the lists and the number of nested levels.

For small lists or lists with a small number of nested levels, using a for loop, a comprehension, or the built-in functions like itertools.chain() or functools.reduce() should provide acceptable performance.

However, for large lists or lists with many nested levels, using the built-in functions itertools.chain() or functools.reduce() may provide a more memory-efficient solution by avoiding the creation of a fully flattened list in memory. These methods operate lazily, yielding the flattened data on the fly instead of storing it all at once.

It’s recommended to benchmark different methods on your specific data and consider the trade-offs between time and memory efficiency.

Flattening Python Lists for Data Science With NumPy

If you’re working with data science tasks in Python, you can leverage the power of the NumPy library to flatten lists efficiently. NumPy provides a flatten() method that can be applied to arrays, which allows you to achieve the same result as flattening a list of lists.

Here’s an example:

import numpy as np
matrix = np.array([
[9, 3, 8, 3],
[4, 5, 2, 8],
[6, 4, 3, 1],
[1, 0, 4, 5],
])
flattened = matrix.flatten()
print(flattened)

The output will be the same:

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

In this example, the NumPy library is imported as np. The matrix is converted to a NumPy array using np.array(), and then the flatten() method is applied to it, returning a flattened array.

Working with NumPy arrays can provide additional benefits, such as efficient memory management and vectorized operations.

Conclusion

Flattening a list of lists in Python is a common operation when working with nested data structures. In this tutorial, you learned several methods to achieve this, including using a for loop, a comprehension, and various built-in functions and standard-library tools.

When choosing a method, consider the size of your data, the number of nested levels, and the trade-offs between time and memory efficiency. NumPy arrays can also be a powerful tool for flattening lists in data science tasks.

Now you have a variety of techniques at your disposal to flatten your lists and work with one-dimensional data structures in Python.