Answers for "convert 2 level nested list to one level list in python"

0

convert 2 level nested list to one level list in python

>>> from collections import Iterable
>>> def flat(lst):
...     for parent in lst:
...         if not isinstance(i, Iterable):
...             yield parent
...         else:
...             for child in flat(parent):
...                 yield child
...
>>> list(flat(([1,[2,2,2],4]))
[1, 2, 2, 2, 4]
Posted by: Guest on May-04-2020

Python Answers by Framework

Browse Popular Code Answers by Language