Python

Flatten the list of list in Python

In this article, we will see how we can flatten the list of the list in different ways. Before proceeding, we have to know about the difference between shallow flattening and deep flattening.

Shallow flattening: This means flattening the list to one depth level only.

Deep flattening: This means flattening the list to any depth level.

The topics which we are going to discuss in this article are as below:

Method 1: Using for loop

In this method, we will use for-loop, which is very common in every programming language. We iterate each element in the list and then iterate that element further, as shown in the program below.

lst = [[30,7],[8,9],[30,7],[8,9]]
flatten_list = []
for i in lst:
  for item in i:
    flatten_list.append(item)
print("list before flattening", lst)
print ("flattened list: ",flatten_list)

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 1: We created a list of lists.

Line 3 to 5: In this line, we are running a nested for loop. The outer for loop is for the main list, and the inner for loop is for the elements. If you see this nested loop, then you will find out that each element from the sub-element list was extracted and append to the new list (flatten_list). In this way, every element in the sub-list is now separated.

Line 6 to 7: These lines showing the original list before and after flattening the list.

Method 2: Using list comprehension

The list comprehension is an advanced method of the above for loop where we write everything in a single line.

lst = [[30,7],[8,9],[30,7],[8,9]]
print("list before flattening", lst)
print ("list after flattening",[item for i in lst for item in i])

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9]]
list after flattening [30, 7, 8, 9, 30, 7, 8, 9]

Line 1: We created a list of the list.
Line 3: This line running two loops in a single line to flatten the list.

Method 3: Using flatten () method

Another method is to use the library flatten () as shown in the program given below. But the flatten method will work only to one level of the nested list; if there are deeply nested lists, then it will not flatten the list. Both single nested and deeply nested program examples are given below.

from iteration_utilities import flatten
lst = [[30,7],[8,9],[30,7],[8,9]]
print(list(flatten(lst)))

Output:

[30, 7, 8, 9, 30, 7, 8, 9]

Line 3: We call the method flatten and pass the list into that as an argument. The above output shows that our list of lists is now flattened.

Now, we are going to see a deeply nested list for the flatten method.

from iteration_utilities import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print(list(flatten(lst)))

Output:

[30, 7, 8, 9, 30, 7, 8, 9, [2]]

Line 2: We created a nested list and also added another element [[2]] that is deeply nested.

Line 3: We call the method flatten and pass the list into that as an argument. The above output shows that it is not done to flatten the deeply nested list fully because the element [2] is still inside a list.

Method 4: Using the deepflatten () method

The other method is the deepflatten () which can flatten the deeply nested list, which is not done by the flatten method as we have seen in the above example.

from iteration_utilities import deepflatten
lst = [[30,7],[8,9],[30,7],[8,9],[ [200 ] ]]
print("list lst before flattening", lst)
flatten_lst = list(deepflatten(lst))
print("list lst after flattening", flatten_lst)

Output:

list lst before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[200]]]
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, 200]

Line 1: We import the deepflatten method.

Line 4: We call the method deepflatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.

Method 5: Using pandas flatten () method

This method does the flatten list even if the list is deeply nested.

from pandas.core.common import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9, 2]

Line 4: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.

Method 6: Using matplotlib flatten () method

This method does the flatten list even if the list is deeply nested.

from matplotlib.cbook import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9, 2]

Line 4: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.

Method 7: Using the unipath flatten () method

This method does the flatten list even if the list is deeply nested.

import unipath
from unipath.path import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9, 2]

Line 5: We call the method flatten and pass the deeply nested list into that as an argument. The above output shows that our deeply nested list is now flattened.

Method 8: Using setuptools flatten () method

This method does the flatten list to only one level.

from setuptools.namespaces import flatten
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9, [2]]

Line 2: We created a nested list and also added another element [[2]] that is deeply nested.

Line 4: We call the method flatten and pass the list into that as an argument. The above output shows that it is not done to flatten the deeply nested list fully because the element [2] is still inside a list.

Method 9: Using the itertools.chain method

To unpack the list of lists, we can also use the itertools.chain method. This method further has two ways to flatten the list of lists. Both methods are given below. These methods also faltten the list of list to one level only.

Using the itertools.chain.from_iterable

import itertools
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list lst before flattening", lst)
flatten_lst = list((itertools.chain.from_iterable(lst)))
print("list lst after flattening", flatten_lst)

Output:

list lst before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, [2]]

Line 2: We created a nested list and also added another element [[2]] that is deeply nested.

Line 4: We call the method itertools.chain.from_iterable() and pass the list into that as an argument. The above output shows that it is not done to fully flatten the deeply nested list because the element [2] is still inside of a list.

Using the * operator

import itertools
lst = [[30,7],[8,9],[30,7],[8,9],[[2]]]
print("list lst before flattening", lst)
flatten_lst = list((itertools.chain(*lst)))
print("list lst after flattening", flatten_lst)

Output:

list lst before flattening [[30, 7], [8, 9], [30, 7], [8, 9], [[2]]]
list lst after flattening [30, 7, 8, 9, 30, 7, 8, 9, [2]]

Method 10: Using numpy.ravel () method

The other method is numpy.ravel which also flattens the nested list. But this method does flatten to one level of the nested.

import numpy as np
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.ravel()
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30  7]
 [ 8  9]
 [30  7]
 [ 8  9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 3: We call the method numpy ravel. The above output shows that our nested list array is now flattened.

Method 11: Using the numpy reshape () method

The other method is numpy reshape, which also flattens the nested list. But this method does flatten to one level of the nested.

import numpy as np
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.reshape(-1)
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))
list before flattening [[30  7]
 [ 8  9]
 [30  7]
 [ 8  9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 3: We call the method reshape(-1). The above output shows that our nested list array is now flattened.

Method 12: Using the numpy flatten () method

The other method is numpy flatten (), which also flattens the nested list. But this method does flatten to one level of the nested.

import numpy as np
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = lst.flatten()
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))


Output:

list before flattening [[30  7]
 [ 8  9]
 [30  7]
 [ 8  9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 5: We call the method flatten. The above output shows that our nested list array is now flattened.

Method 13: Using numpy.concatenate () method

The other method is numpy.concatenate (), which also flattens the nested list. But this method does flatten to one level of the nested.

import numpy as np
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = list(np.concatenate(lst))
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30  7]
 [ 8  9]
 [30  7]
 [ 8  9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 3: We call the method numpy.concatenate () and pass the nested list array into that as an argument. The above output shows that our nested list array is now flattened.

Method 14: Using the numpy flat method

The other method is numpy flatwhich also flattens the nested list. But this method does flatten to one level of the nested.

import numpy as np
lst = np.array([[30,7],[8,9],[30,7],[8,9]])
flatten_lst = list(lst.flat)
print("list before flattening", lst)
print ("flattened list: ",list(flatten(lst)))

Output:

list before flattening [[30  7]
 [ 8  9]
 [30  7]
 [ 8  9]]
flattened list:  [30, 7, 8, 9, 30, 7, 8, 9]

Line 3: We call the method flat, and the above output shows that our nested list array is now flattened.

Conclusion:

In this blog, we have shown you different methods that we can use to flatten our list of lists. All the above methods work perfectly for the one level of the nested list. But if there are deeply nested lists, some of the above methods work perfectly. So, it’s up to you and, according to your program requirements which method you want to use.

The code for this article is also available at the Github link:
https://github.com/shekharpandey89/flatten-the-list-of-list

About the author

Shekhar Pandey