Skip to content

Demystifying Python List of Lists

[

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

To flatten a list of lists, you can follow these steps using a for loop:

  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.

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, a new empty list flat_list is created. This list is used to store the flattened data when it is extracted from the matrix. Then, a loop is started to iterate over the inner, or nested, lists from matrix. In this example, the name row is used to represent the current nested list.

In every iteration, the .extend() method is used 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.

To flatten the matrix using the flatten_extend() function, you can call it like this:

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

This will output:

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

Using a Comprehension to Flatten a List of Lists

Another way to flatten a list of lists in Python is by using a list comprehension. Here’s an example:

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

This will also output:

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

In this example, a list comprehension is used to iterate over each sublist in the matrix and append each item to the flat_list. The result is a flattened list.

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

Python provides several built-in tools and modules in the standard library that can be used to flatten a list. Here are a few examples:

Chaining Iterables With itertools.chain()

The itertools.chain() function can be used to combine multiple iterables into a single iterable, effectively flattening the list. Here’s an example:

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

This will output the same flattened list:

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

Concatenating Lists With functools.reduce()

The functools.reduce() function can be used to concatenate multiple lists into a single list. Here’s an example:

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

This will also output the flattened list:

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

Using sum() to Concatenate Lists

The sum() function can also be used to concatenate multiple lists. Here’s an example:

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

Again, this will output the flattened list:

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

Considering Performance While Flattening Your Lists

When working with large lists or nested lists, it’s important to consider performance when flattening the lists. Some methods may be faster than others depending on the size of the data.

For example, using the itertools.chain() method is generally more memory-efficient compared to the list comprehension or functools.reduce() method. However, the difference in performance may be insignificant for small datasets.

It’s recommended to test different methods and measure the execution time for your specific use case to determine the most efficient approach.

Flattening Python Lists for Data Science With NumPy

If you’re working with data science and handling multidimensional arrays or matrices, the NumPy library provides efficient methods for flattening lists and arrays. 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],
])
flat_array = matrix.flatten()
print(flat_array.tolist())

This will output the flattened array as a list:

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

The flatten() method from NumPy returns a one-dimensional array. To convert it back to a list, you can use the .tolist() method.

Conclusion

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

Experiment with different methods and consider performance when working with large datasets. Python provides versatile tools to help you flatten lists efficiently and effectively.