This is only for self learning of the concept and might not have practical use
My question is
can I use only recursive function and list comprehension to flatten a unknown level of nested list?
If 1 is possible, can I only use list comprehension + lambda function to get the same purpose?
So far this all I can get but it seems not working.
l=[1,[2,3],[4,5,[6,7,8,9]]] # assuming unknown level of nesting
def fun_d(x):
return [fun_d(e) if isinstance(e,list) else e for e in x]
fun_d(l)
Out[25]: [1, [2, 3], [4, 5, [6, 7, 8, 9]]]
You can though it is a little strange:
You may want to use
Sequence
rather thanlist
so other types of sequences would also be flattened.A named lambda is trivial, for a truly anonymous
lambda
you can use ay-combinator
And just to show how ridiculous this is, an anonymous recursive
lambda
: