Flatten Nested List In Python

Lists are popular data types used in Python programming language. Lists can store multiple items with different data types. By using a single list variable multiple items can be stored, used, and manipulated easily in a list. List items are separated with command where a list may contain single items or thousands of items. The List can also sore another list as an item or tuple which can contain multiple items too. In this tutorial, we will learn how to flatten a nested list?

List of List or Nested Lists

Before working on flattening or converting a flat list we should learn nested lists. A nested list means a list that contains at least one item which is a list, tuple, or dictionary. As List, tuple and dictionary are lists like structured they can contain multiple items too.

mylist = [ 1 , 2 , "ali" , "ahmet" , [ 1 , 2 , 3 ] , ["a" , "b" , "c" , [ 10 , 11 , 12 ] ] ]

List Comprehension

The most popular and easy way to convert a nested list into a flat list is by using the list comprehension method. The list comprehension method is easy to implement with a single line of code and in an elegant and understandable way. But keep in mind that our list is two-level and each item of the main list is a list too. This will not work with variable level lists.

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

flat_list = [ item for var_list in mylist for item in var_list]

print(flat_list)
List Comprehension

Deepflatten Module

In the previous example, we have flattened a nested list where all items of the parent list are list too. If the list is more than 2 level we should improve previous list comprehension which will be complex or use another way. Deepflatten is a method provided with the iteration-utilities package which is not installed by default with Python. deepflatten() method will convert the given nested list into a flat list. We can install the iteration-utilities with the following pip command.

$ pip3 install iteration-utilities

Then we will use the deepflatten() method by importing the iteration_utilities module like below.

from iteration_utilities import deepflatten

mylist = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , [ 8 , 9 ] ] ]

flat_list = deepflatten(mylist)

print(flat_list)

Recursively Iteration with for loop

We can use our friend for loop in order to recursively iterate over the given list and sublists. We will use multiple nested for loops where the for loop cunt is related with the nested list levels. If there is 2 levels we will use 2 nested for loops , if there is 4 level of nested list we will use 4 nested for loops.

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

flat_list = []

for i in range(len(mylist)):
   for j in range(len(mylist[i])):
      flat_list.append(mylist[i][j])

print(flat_list)
Recursively Iteration with for loop

Flatten with reduce() Method

The functools module provides the reduce method which can be used to convert nested list into a flat list. We will specify the operator.iconcat method for the reduce where items will be selected one by one and put a simple and flat list.

import functools
import operator

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

flat_list = functools.reduce(operator.iconcat, mylist , [])

print(flat_list)
Flatten with reduce() Method

Leave a Comment