Python | Ways to flatten a 2D list

03 Aug, 2021
Given a 2D list, write a Python program to convert the given list into a flattened list. 
Method #1: Using chain.iterable() 
Python3
# Python code to demonstrate# converting 2d list into 1d list# using chain.from_iterables # import chainfrom itertools import chain ini_list = [[1, 2, 3],            [3, 6, 7],            [7, 5, 4]]             # printing initial listprint ("initial list ", str(ini_list)) # converting 2d list into 1d# using chain.from_iterablesflatten_list = list(chain.from_iterable(ini_list)) # printing flatten_listprint ("final_result", str(flatten_list))
initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
 
Method #2: Using list comprehension 
Python3
# Python code to demonstrate# converting 2d list into 1d list# using list comprehension # import chainfrom itertools import chain ini_list = [[1, 2, 3],            [3, 6, 7],            [7, 5, 4]]             # printing initial listprint ("initial list ", str(ini_list)) # converting 2d list into 1d# using list comprehensionflatten_list = [j for sub in ini_list for j in sub] # printing flatten_listprint ("final_result", str(flatten_list))
initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
 
Method #3: Using functools.reduce  
Python3
# Python code to demonstrate# converting 2d list into 1d list# using functools.reduce # import functoolsfrom functools import reduce ini_list = [[1, 2, 3],            [3, 6, 7],            [7, 5, 4]]             # printing initial listprint ("initial list ", str(ini_list)) # converting 2d list into 1d# using functools.reduceflatten_list = reduce(lambda z, y :z + y, ini_list) # printing flatten_listprint ("final_result", str(flatten_list))
initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
 
Method #4: Using sumsum has an optional argument: sum(iterable [, start])
Python3
# Python code to demonstrate# converting 2d list into 1d list# using sum ini_list = [[1, 2, 3],            [3, 6, 7],            [7, 5, 4]] # printing initial listprint ("initial list ", str(ini_list)) # converting 2d list into 1dflatten_list = sum(ini_list, []) # printing flatten_listprint ("final_result", str(flatten_list)) # This code is contributed by# Mayank Chaudhary - chaudhary_19
Output: 
initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
Method #5: Using lambda  
Python3
#Python 3 code to flatten nested list#contributed by S Lakshman Rao - kaapalxini_list=[[1, 2, 3],          [3, 6, 7],          [7, 5, 4]] #Using lambda flatten_list = lambda y:[x for a in y for x in flatten_list(a)] if type(y) is list else [y] print("Initial list ",ini_list) #printing initial list print("Flattened List ",flatten_list(ini_list)) # printing flattened list
Output: 
Initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List  [1, 2, 3, 3, 6, 7, 7, 5, 4]
Method #6: Using numpy  
Python3
#Python 3 code to flatten nested list#Contributed by S Lakshman Rao - kaapalximport numpy ini_list=[[1, 2, 3],          [3, 6, 7],          [7 ,5, 4]] print("Initial list ",ini_list) #Printing Initial list print("Flattened List ",list(numpy.concatenate(ini_list).flat))#Using numpy to flatten list and printing the result
Output: 
Initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List  [1, 2, 3, 3, 6, 7, 7, 5, 4]
 
chaudhary_19
kaapalx
arorakashish0911
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary