Skip to content

Flatten a Python List-In-List in Effortless Steps

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:

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)

The output should be:

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

Congratulations! You have successfully flattened a list of lists using a for loop and the .extend() method.

Using a Comprehension to Flatten a List of Lists

Next, you can explore another powerful technique in Python: list comprehensions. List comprehensions provide a concise way to create new lists based on existing lists.

To flatten a list of lists using a comprehension, you can use nested loops and a simple syntax. Here’s how you can rewrite the previous example using a comprehension:

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

The above comprehension reads as follows: “For every sublist in the matrix, take every item in the sublist and add it to the result list”. The result list is created automatically by the comprehension.

You can now test the flatten_comprehension() function in the same way as before:

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]

Congratulations! You have successfully flattened a list of lists using a comprehension.

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

Python provides several standard-library and built-in functions that can help you flatten a list of lists without explicitly writing for loops or list comprehensions. Let’s take a look at three of these tools: itertools.chain(), functools.reduce(), and sum().

Chaining Iterables With itertools.chain()

The itertools.chain() function is part of the itertools module and allows you to chain multiple iterables together as if they were a single iterable. In the context of flattening a list of lists, you can use itertools.chain() to chain all the inner lists together and obtain a single iterator.

Here’s an example that demonstrates how to use itertools.chain() to flatten a list of lists:

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

In the flatten_chain() function, you pass *matrix as the argument to itertools.chain(). This syntax, called the “splat” operator or “unpacking” operator, unpacks the elements of matrix and passes them as individual arguments to itertools.chain(). The result is a single iterator that contains all the items from the inner lists.

To obtain a list, you wrap the itertools.chain() call in the list() function.

You can now test the flatten_chain() function as before:

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]

Congratulations! You have successfully flattened a list of lists using itertools.chain().

Concatenating Lists With functools.reduce()

The functools.reduce() function is part of the functools module and allows you to perform a rolling computation on a sequence by applying a specified binary operator. In this case, you can use functools.reduce() to concatenate all the inner lists into a single list.

Here’s an example that demonstrates how to use functools.reduce() to flatten a list of lists:

import functools
def flatten_reduce(matrix):
return functools.reduce(lambda x, y: x + y, matrix)

In the flatten_reduce() function, you pass a lambda function as the first argument to functools.reduce(). The lambda function takes two arguments, x and y, and performs the concatenation operation x + y. The functools.reduce() function applies this lambda function successively to the elements of matrix, resulting in a single list that contains all the items from the inner lists.

You can now test the flatten_reduce() function as before:

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]

Congratulations! You have successfully flattened a list of lists using functools.reduce().

Using sum() to Concatenate Lists

Lastly, you can leverage the sum() function to concatenate all the inner lists together. The sum() function has an optional second argument, called the “start value”, which defaults to 0. When you pass a list to sum(), it performs addition by default.

Here’s an example that demonstrates how to use sum() to flatten a list of lists:

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

In the flatten_sum() function, you pass an empty list [] as the second argument to sum(). This tells sum() to start with an empty list as the base for concatenation. The result is a single list that contains all the items from the inner lists.

You can now test the flatten_sum() function as before:

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]

Congratulations! You have successfully flattened a list of lists using sum().

Considering Performance While Flattening Your Lists

When working with large datasets or performance-critical applications, the choice of flattening technique can impact the overall performance of your code. While the techniques discussed so far are suitable for most scenarios, you might want to consider their performance implications for your specific use case.

For instance, if you’re dealing with very large lists of lists and performance is a concern, using the itertools.chain() function might be more efficient, as it provides an iterator-based approach that doesn’t require creating an intermediate list. On the other hand, if your matrix is relatively small or the performance impact is negligible, using a list comprehension or one of the built-in functions should suffice.

Always keep in mind the volume of data you’re working with and measure the performance of your code using the appropriate tools and techniques, such as profiling or benchmarking.

Flattening Python Lists for Data Science With NumPy

If you work with data science and numerical operations in Python, you’re likely familiar with the NumPy library. NumPy provides efficient arrays and numerical operations that are optimized for performance.

When it comes to flattening a list of lists for data science purposes, you can leverage the power of NumPy. NumPy provides the flatten() and ravel() methods that can be used to flatten arrays, including multidimensional arrays.

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

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

In the flatten_numpy() function, you first convert the matrix into a NumPy array using np.array(matrix). Then you call the flatten() method on the array to obtain a flattened array. Finally, you convert the flattened array back to a regular Python list using .tolist().

You can now test the flatten_numpy() function as before:

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]

Congratulations! You have successfully flattened a list of lists using NumPy.

Conclusion

Flattening a list of lists in Python is a common operation when working with nested data. In this tutorial, you learned several techniques to flatten a list of lists, including using a for loop with .extend(), list comprehensions, and various standard-library and built-in functions such as itertools.chain(), functools.reduce(), sum(), and NumPy.

Depending on your specific use case and performance requirements, you can choose the most suitable technique to flatten your lists of lists. Consider the volume of data you’re working with and measure the performance of your code when performance is critical.

Now you have the tools and knowledge to flatten any list of lists in Python!