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. The order of the elements in the flattened list of lists remains the same.
For example, let’s take the list of lists below:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The nested_list
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 over each item in those sublists. This basic approach, which uses 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 transform a list of lists into a list:
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)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
In this code, I’m using nested for loops to flatten the list. To start, I create a new empty list called flattened_list
.
Then I use the outer loop to iterate through the sublists and the inner loop to iterate over each item in a given sublist. Each item in a sublist is appended to the flattened_list
.
How to Flatten List of Lists Using a List Comprehension?
A list of lists can be flattened using Python’s list comprehension. Using a list comprehension is a concise 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.
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)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
The list comprehension iterates through each sublist in the nested_list
and then iterates through each item
in every sublist
just like in the previous example that was using for loops.
The main difference between a list comprehension and a for loop is that a list comprehension does everything in one line, making the code more concise 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 to 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)
We first import the NumPy module and 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'>
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 data structure 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 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 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 transform a list of lists into a list.
Here is an example of how to do that:
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 data structure 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 approaches so feel free to go back and review this section until you fully understand it.
Related article: do you want to learn more about Python lists? Have a look at the CodeFatherTech tutorial about Python list methods.
Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design. He is a professional certified by the Linux Professional Institute.
With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for robotics with Raspberry Pi.