Python | Ways to flatten a 2D list

Given a 2D list, write a Python program to convert the given list into a flattened list.

Method #1: Using chain.iterable()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# converting 2d list into 1d list
# using chain.from_iterables
  
# import chain
from itertools import chain
  
ini_list = [[1, 2, 3],
            [3, 6, 7],
            [7, 5, 4]]
              
# printing initial list
print ("initial list ", str(ini_list))
  
# converting 2d list into 1d
# using chain.from_iterables
flatten_list = list(chain.from_iterable(ini_list))
  
# printing flatten_list
print ("final_result", str(flatten_list))

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# converting 2d list into 1d list
# using list comprehension
  
# import chain
from itertools import chain
  
ini_list = [[1, 2, 3],
            [3, 6, 7],
            [7, 5, 4]]
              
# printing initial list
print ("initial list ", str(ini_list))
  
# converting 2d list into 1d
# using list comprehension
flatten_list = [j for sub in ini_list for j in sub]
  
# printing flatten_list
print ("final_result", str(flatten_list))

chevron_right


Output:



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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# converting 2d list into 1d list
# using functools.reduce
  
# import functools
from functools import reduce
  
ini_list = [[1, 2, 3],
            [3, 6, 7],
            [7, 5, 4]]
              
# printing initial list
print ("initial list ", str(ini_list))
  
# converting 2d list into 1d
# using functools.reduce
flatten_list = reduce(lambda z, y :z + y, ini_list)
  
# printing flatten_list
print ("final_result", str(flatten_list))

chevron_right


Output:

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 sum
sum has an optional argument: sum(iterable [, start])

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 list 
print ("initial list ", str(ini_list))
  
# converting 2d list into 1d
flatten_list = sum(ini_list, [])
  
# printing flatten_list 
print ("final_result", str(flatten_list))
  
# This code is contributed by
# Mayank Chaudhary - chaudhary_19

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python 3 code to flatten nested list
#contributed by S Lakshman Rao - kaapalx
ini_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) #priniting initial list
  
print("Flattened List ",flatten_list(ini_list)) # printing flattened list

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python 3 code to flatten nested list
#Contributed by S Lakshman Rao - kaapalx
import 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

chevron_right


Output:

Initial list  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List  [1, 2, 3, 3, 6, 7, 7, 5, 4]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : chaudhary_19, kaapalx