Given a 2D list, Write a Python program to convert the given list into a flattened list.
Method #1: Using chain.iterable()
Python3
from itertools import chain
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
print ( "initial list " , str (ini_list))
flatten_list = list (chain.from_iterable(ini_list))
print ( "final_result" , str (flatten_list))
|
Output:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
The time complexity of the code is O(m*n) where m is the number of lists and n is the maximum length of the lists.
The auxiliary space complexity of the code is O(mn) because a new list of size mn is created to store the flattened list.
Python3
from itertools import chain
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
print ( "initial list " , str (ini_list))
flatten_list = [j for sub in ini_list for j in sub]
print ( "final_result" , str (flatten_list))
|
Output:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
The time complexity of this program is O(mn), where m is the number of rows and n is the number of columns in the input 2D list.
The space complexity of this program is O(mn), as the size of the output list is proportional to the size of the input list.
Method #3: Using functools.reduce
Python3
from functools import reduce
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
print ( "initial list " , str (ini_list))
flatten_list = reduce ( lambda z, y :z + y, ini_list)
print ( "final_result" , str (flatten_list))
|
Output:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
The time complexity of the above code is O(N^2) as it has to traverse all the elements in a 2D list once.
The space complexity is also O(N^2), as the entire input list needs to be stored in memory and a new list is created to store the flattened version of the list.
Method #4: Using sum
sum has an optional argument: sum(iterable [, start])
Python3
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
print ( "initial list " , str (ini_list))
flatten_list = sum (ini_list, [])
print ( "final_result" , str (flatten_list))
|
Output:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
Time Complexity: The time complexity of this program is O(mn), where m is the number of rows and n is the number of columns in the input 2D list.
Auxiliary Space: The auxiliary space of this program is O(mn), as the size of the output list is proportional to the size of the input list.
Method #5: Using lambda
Python3
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
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)
print ( "Flattened List " ,flatten_list(ini_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
import numpy
ini_list = [[ 1 , 2 , 3 ],
[ 3 , 6 , 7 ],
[ 7 , 5 , 4 ]]
print ( "Initial list " ,ini_list)
print ( "Flattened List " , list (numpy.concatenate(ini_list).flat))
|
Output:
Initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List [1, 2, 3, 3, 6, 7, 7, 5, 4]
The time complexity of this program is O(N^2) where N is the number of elements in the nested list.
The auxiliary space complexity is O(N), as the flattened list is stored in memory as a new list.
Method #7: Using extend() method
Python3
ini_list = [[ 1 , 2 , 3 ],[ 3 , 6 , 7 ],[ 7 , 5 , 4 ]]
print ( "Initial list " ,ini_list)
res = []
for i in ini_list:
res.extend(i)
print ( "Flattened List " ,res)
|
Output
Initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List [1, 2, 3, 3, 6, 7, 7, 5, 4]
Please Login to comment...