How to Flatten a List in Python

  1. Python list shallow flattening
  2. Python list deep flattening

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 [[[1], [2]], [3, 4]]. Such a list is said to contain 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 list shallow flattening

A simple approach to understand and visualize is to use a double for loop.

The first 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 expressiveness of Python allows us to achieve the same goal in just one line. Here Python’s list comprehensions 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 itertools.chain().

>>> 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 Pandas - 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 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 iteration-utilities 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]

Related Article - Python List

  • What is the difference between list methods append and extend
  • How to Convert a List to String in Python
  • How to concatenate two or multiple lists in Python
  • What is Difference Between del, remove And pop on Python Lists
  • How to check whether a value exists in Python list in a fast way
  • How to Deduplicate a List in Python
  • How to Create a List with a Specific Size in Python
  • comments powered by Disqus