Convert Nested List To A Flat List In Python

2 min read

Lists are probably the most used data type in Python. Lists being mutable offer a lot of flexibility to perform a number of operations on the items of the list.

A list contains items separated by commas and enclosed within square brackets [ ].  Lists in Python can also have items of different data types together in a single list. A nested list is nothing but a list containing several lists for example [1,2,[3],[4,[5,6]]

Often we need to convert these nested list into a flat list to perform regular list operations on the data. Fortunately, in Python, there are many ways to achieve this.

In this tutorial, we will go through several methods to convert a Nested list into a flat regular list.

Convert Nested List To A Flat List In Python

def flatten(li):
    return sum(([x] if not isinstance(x, list) else flatten(x)
                for x in li), [])

print(flatten([1, 2, [3], [4, [5, 6]]]))

Output:

[1, 2, 3, 4, 5, 6]

Flatten List using Inbuilt reduce Function

Method 1:

import functools
import operator

li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]]
flat_list = functools.reduce(operator.concat, li)
print(flat_list)

Output:

[14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]

Method 2:

from functools import reduce

li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]]
flat_list = reduce(lambda x, y: x+y, li)
print(flat_list)

Output:

[14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]

Method 3:

This is one of the most efficient ways to flatten a large list.

from itertools import chain

li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]]
flat_list = list(chain.from_iterable(li))
print(flat_list)

Output:

[14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]

Flatten List Using List Compression

li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]]

flat_list = [item for sublist in li for item in sublist]
print(flat_list)

Output:

[14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]

Flatten List Using NumPy

Note that, NumPy doesn't come bundled with Python Installation you have to install it which can be done simply using Pip Installer.

pip install numpy

Once the installation is done you can run the code below,

import numpy

li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]]

flat_list = list(numpy.concatenate(li).flat)
print(flat_list)

Output:

[14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]

PYTHON

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read