6 Ways to Flatten a List of Lists to a List in Python — CODEFATHER

Claudio Sabato
8 min readMay 17, 2023

Do you need to flatten a list of lists in Python? In this tutorial, we will examine techniques to flatten lists in Python.

Knowing how to flatten a list of lists into a single list is a common task. You can flatten a Python list using techniques such as list comprehension, the NumPy library, and for loops. The result of flattening a list of lists is a single list with no sublists.

Let’s learn how to flatten lists using Python!

What Does Flattening a Python List Mean?

Python, a robust and versatile programming language, offers programmers a number of approaches to common programming challenges.

In Python, “flattening” a list means transforming a multidimensional list or a list of lists into a one-dimensional list. It is the technique to simplify the list structure while maintaining the consistency and order of the data structure. In other words, the order of the elements in the flattened list of lists remains the same.

For example, let’s take an example of list of lists below:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The nested_list above contains three sub-lists (three lists inside the main list).

Flattening the list means converting the list of lists above into a one-dimensional list as shown below:

flattened_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

So, what Python code do you have to write to accomplish this?

How Do You Flatten a List of Lists to a List in Python Using a Nested For Loop?

The for loop in Python flattens a list of lists by iterating over each sublist and then each item within those sublists. This basic approach, which takes advantage of Python’s built-in looping mechanism, converts a nested list structure into a flat list.

The following example shows how you can use a for loop to flatten a list of lists:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = []

for sublist in nested_list:
for item in sublist:
flattened_list.append(item)

print(flattened_list)

Here is the output of the Python code above:

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

In this code, we are using nested for loops to flatten the list.

To start, we create a new list called flattened_list that is initially empty.

We use the outer loop to iterate through the sublists and the inner loop iterates over each item in a given sublist. Each item in a sublist is appended to the flattened_list.

We then use the print function to print the value of the flattened_list.

How to Flatten List of Lists Using a List Comprehension?

Let’s have a look at another way to flatten a list of lists.

A list of lists can be flattened using Python’s list comprehension. Using a list comprehension is a condensed method of making lists, and it is frequently more effective than utilizing loops.

If you are just getting started with Python, you might find this method a bit harder to understand compared to the previous one based on for loops.

Ready for an example?

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [item for sublist in nested_list for item in sublist]
print(flattened_list)

The output is the flat list we expect:

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

Once again, in the code snippet above, we are using a list comprehension to flatten a list of lists. The list comprehension iterates through each sublist in the nested_list and then it iterates through each item in every sublist just like in the previous for loop example.

The main difference between a list comprehension and the for loop is that a list comprehension does everything in one line, making the code more compact and Pythonic.

Here is a tutorial to get more familiar with Python list comprehensions.

How Do You Flatten a List of Lists Using NumPy?

A powerful tool in Python’s data science toolbox is the NumPy package. Its many features include the capability to flatten multi-dimensional arrays.

Let’s take an example of how we can use NumPy to flatten a multi-dimensional array. Conceptually a NumPy multi-dimensional array is similar to a list of lists.

import numpy as np

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
nested_array = np.array(nested_list)
flattened_list = nested_array.flatten().tolist()
print(flattened_list)

As you can see, it’s relatively easy to flatten a list of lists using NumPy.

We first import the NumPy module and we use “import numpy as np” so that we can use np in our program when we want to call a function in the NumPy library.

Then we convert the list of lists into a NumPy array. Let’s see the type of these two data structures using the type() built-in function.

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Nested list type:", type(nested_list))

nested_array = np.array(nested_list)
print("Nested array type:", type(nested_array))

In the output you can see the two different data types:

Nested list type: <class 'list'>
Nested array type: <class 'numpy.ndarray'>

After obtaining a multi-dimensional array we use the flatten() method to flatten it and then we convert the NumPy array to a list using the tolist() method.

flattened_list = nested_array.flatten().tolist()

Here is the final output of the Python code:

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

I’m curious to see what happens if we remove the tolist() function call:

flattened_list = nested_array.flatten()

[output]
[1 2 3 4 5 6 7 8 9]

You can see that the output is similar to the flattened list printed previously with the difference that the elements are not separated by commas.

Using the type() function you will also see that the data returned is a NumPy array because we have removed the call to the tolist() function that was converting the array into a list.

print(type(flattened_list))

[output]
<class 'numpy.ndarray'>

Are there more ways to create a flat list in Python?

Let’s find out!

Flatten List Using sum() Python Built-in Function

You can also use the sum() function to make a flattened list in Python. The sum() function is a Python built-in function that is used to add all the items in a list. It can be used with numerical data types like integers and floats and it can also be used with lists. When used with lists, sum() concatenates lists together.

Let’s start from the same list of lists we have used in the previous examples:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = sum(list_of_lists, [])
print(flattened_list)

The sum() function takes two arguments:

  • the first argument is the iterable to be “summed” (the list of lists)
  • the second argument is a start value. The start value is optional and defaults to 0. However, since we’re working with lists instead of numbers, we have to provide a start value that matches the type of the iterable. That’s why, we provide an empty list ([]) as the start value.

The sum() function then concatenates each list in list_of_lists with this start value, effectively flattening the list of lists.

See the output below:

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

Quite simple and effective!

Using Itertools Module to Flatten a Python List

Another way to flatten a Python list is by using the itertools module.

We can use the itertools.chain class that has one method called from_iterable().

print(itertools.chain)
print(itertools.chain().from_iterable)

[output]
<class 'itertools.chain'>
<built-in method from_iterable of type object at 0x104985a68>

Let’s take a list of lists and pass it to itertools.chain.from_iterable(). To get the flat list we have to pass the output returned to the list() function.

import itertools

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = list(itertools.chain.from_iterable(list_of_lists))
print(flattened_list)

Confirm that the output is correct by executing the Python code above:

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

As an alternative, you can just import chain from itertools. Here is how the code changes when we change the import statement.

from itertools import chain

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = list(chain.from_iterable(list_of_lists))
print(flattened_list)

Flatten a List of Lists Using Python Pandas Library

Pandas is a powerful Python library you can use for data manipulation and data analysis. For example, using Pandas you can read CSV files, write CSV files and work with other structured data like databases, and Excel files.

You can use Pandas and the DataFrame data structure to flatten a list of lists.

Here is an example of how to flatten a list of lists using Pandas:

import pandas as pd

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Create a Pandas DataFrame from the list of lists
df = pd.DataFrame(list_of_lists)

# Apply the stack() function to the DataFrame
stacked_df = df.stack()

# Convert the "stacked" DataFrame into a list
flattened_list = stacked_df.tolist()

print(flattened_list)

In the code above, we are first importing the Pandas library and then we initialize a list containing three sublists.

Then we convert the list of lists into a DataFrame. Here is what the value of the df variable looks like:

df = pd.DataFrame(list_of_lists)
print(df)

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

You can see that each sublist in the original list becomes a row in the Pandas dataframe.

The next step is to apply the stack() function to the dataframe. The stack() function reshapes the dataframe by moving column indexes to a new level of inner row indexes. For example, see how the column indexes 0, 1, and 2 are now applied to each row index 0, 1, and 2 as inner indexes.

stacked_df = df.stack()
print(stacked_df)
print(type(stacked_df))

[output]
0 0 1
1 2
2 3
1 0 4
1 5
2 6
2 0 7
1 8
2 9
dtype: int64
<class 'pandas.core.series.Series'>

Spend some time comparing the structure of the data before and after using the stack() function. This will make it clear to you.

Interestingly, as you can see above, applying the stack() function to the DataFrame returns a Pandas Series.

Finally, we use the function tolist() to obtain the final flattened list.

flattened_list = stacked_df.tolist()
print(flattened_list)

[output]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

I know, it’s a bit more complex than the other ways to flatten a list so feel free to go back and review this section until you fully understand it.

Conclusion

A common issue in Python programming is how to combine a list of lists into a single, flat list.

There are several methods you may use to do this activity. In this article, we have seen six different ways you can follow to flatten a list of lists into a single list.

Now you can select the one that best meets your requirements.

Related article: do you want to learn more about Python lists? Have a look at the Codefather tutorial about Python list methods.

Originally published at https://codefather.tech on May 17, 2023.

--

--

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.