Convert Nested List To Flat List
Code
Suppose you have a list like :
[[1], [2,3], [4, [5, 6]], [7], [8], [9], [10]]
Now if I want to make it a flat list like :
[1,2,3,4,5,6,7,8,9]
How can we do that? 😕
Don't worry dear, there are many ways to do so. You are going to take help one builtin lib of python 😍
# Example
from functools import reduce
>>> l = [[1], [2,3], [4, 5, 6], [7], [8], [9], [10]]
>>> flat_list = reduce(lambda x, y: x+y, l)
>>> flat_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Enjoy Edge-cutting Technology 😉
Comments
Post a Comment