Python – Flatten List to individual elements
Sometimes, while working with a Python list, we can have a problem in which we need to perform flatten of the list, i.e convert a mixed list to a flattened one. This can have applications in domains that use 1D lists as input. Let’s discuss certain ways in which this task can be performed.
Flatten List using list comprehension
Here, we are using list comprehension to flatten the list from 2D to 1D.
Python3
res = [i for row in [[ 1 , 3 , "geeks" ], [ 4 , 5 ], [ 6 , "best" ]] for i in row] print (res) |
Output:
[1, 3, 'geeks', 4, 5, 6, 'best']
Flatten List using sum()
Here, we are using sum() function in which we passed test_list as iterable object as first parameter, and second parameter as empty list in which it stores the element.
Python3
test_list = [[ 1 , 3 , "gfg" ], [ 4 , 5 ], [ 6 , "best" ]] test_list = sum (test_list, []) print (test_list) |
Output:
[1, 3, 'gfg', 4, 5, 6, 'best']
Flatten List using loop
The combination of the above functionalities can be used to perform this task. In this, we check for an instance of the list and flatten it and the rest of the elements we add to the list brutely.
Python3
def flatten(test_list): if isinstance (test_list, list ): temp = [] for ele in test_list: temp.extend(flatten(ele)) return temp else : return [test_list] # Initializing list test_list = [ 'gfg' , 1 , [ 5 , 6 , 'geeks' ], 67.4 , [ 5 ], 'best' ] # Flatten List to individual elements # using loop + isinstance() res = flatten(test_list) # printing result print ( "The List after flattening : " + str (res)) |
Output:
The List after flattening : [‘gfg’, 1, 5, 6, ‘geeks’, 67.4, 5, ‘best’]
Flatten List using flatten() method
Pandas flatten return a copy of the array collapsed into one dimension.
Python3
from pandas.core.common import flatten l = [[ 1 , 3 , "gfg" ], [ 4 , 5 ], [ 6 , "best" ]] print ( list (flatten(l))) |
Output:
[1, 3, 'gfg', 4, 5, 6, 'best']
Flatten List using chain() + isinstance()
This is yet another way in which this task can be performed. In this we perform the task of iteration using chain() and checking for list instance is done using isinstance().
Python3
from itertools import chain # Initializing list test_list = [ 'gfg' , 1 , [ 5 , 6 , 'geeks' ], 67.4 , [ 5 ], 'best' ] # Flatten List to individual elements # using chain() + isinstance() res = list (chain( * [ele if isinstance (ele, list ) else [ele] for ele in test_list])) # printing result print ( "The List after flattening : " + str (res)) |
Output :
The List after flattening : [‘gfg’, 1, 5, 6, ‘geeks’, 67.4, 5, ‘best’]
Flatten List using reduce() function
The reduce() function is defined in the “functools” module. It applies a function of two arguments continuously on the given sequence and returns a single value.
Python3
from functools import reduce # Initializing list test_list = [[ 1 , 3 , "gfg" ], [ 4 , 5 ], [ 6 , "best" ]] # Flatten List to individual elements # using reduce() res = reduce ( lambda x,y: x + y, test_list) # printing result print ( "The List after flattening : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The List after flattening : [1, 3, 'gfg', 4, 5, 6, 'best']
Time complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...