We will learn how to flatten a nested list in Python, in layman language, flatten a list of lists in Python.
Flattening is an operation where we take a list of nested lists and convert it into a different data structure that contains no nested lists. Instead, it includes all elements from all lists, which were initially nested. Such a data structure is called a flat or a flattened list.
The original list can contain nested lists in the first level of nestedness only, for example,
[[1, 2], [3, 4]].
Otherwise, it contains lists on the second, third, and further levels of nestedness, for example
[[, ], [3, 4]]. Such a list contains deeply nested lists.
If we want to unnest just a single level of nestedness, we call it shallow flattening. If we want to unnest all levels of nestedness, we call it deep flattening.
Python Flatten List (Shallow Flattening)
A simple approach to understand and visualize is to use a double
for loop traverses nested lists. The second
for loop iterates over elements of each nested list and appends them to a result flat list one by one.
>>> flat_list =  >>> original_list = [[1, 2], [3, 4]] >>> for l in original_list: ... for item in l: ... flat_list.append(item) ... >>> flat_list [1, 2, 3, 4]
The above method is clear and readable, but the comprehension method in Python allows us to achieve the same goal in just one line.
Here Python’s come to help.
>>> original_list = [[1, 2], [3, 4]] >>> flat_list = [item for l in original_list for item in l] >>> flat_list [1, 2, 3, 4]
If you don’t need a flat list per se, but want to be able to iterate over all elements of all sublists as if they were coming from a single flat data structure, you can use .
>>> import itertools >>> original_list = [[1, 2], [3, 4]] >>> iterator = itertools.chain(*original_list) >>> for item in iterator: ... print(item) ... 1 2 3 4
If you use - an open-source package for data analysis in Python - there is a built-in way as well.
>>> from pandas.core.common import flatten >>> original_list = [[1, 2], [3, 4]] >>> flat_list = list(flatten(original_list)) >>> flat_list [1, 2, 3, 4]
Python Flatten List (Deep Flattening)
As mentioned above, shallow flattening will not work, if we have deeply nested lists. It will unnest just the first level of nestedness, as shown in the example below.
>>> deeply_nested_list = [[[1, 2], 3], [4, 5, 6]] >>> flat_list = [item for l in deeply_nested_list for item in l] >>> flat_list [[1, 2], 3, 4, 5, 6]
To do the deep flattening, use
iteration_utilities.deepflatten() from the PyPI package.
First, install the PyPI package itself.
$ pip install iteration-utilities
Then flatten your deeply nested lists.
>>> from iteration_utilities import deepflatten >>> deeply_nested_list = [[[1, 2], 3], [4, 5, 6]] >>> flat_list = list(deepflatten(deeply_nested_list)) >>> flat_list [1, 2, 3, 4, 5, 6]