convert 2 level nested list to one level list in python

Solutions on MaxInterview for convert 2 level nested list to one level list in python by the best coders in the world

showing results for - "convert 2 level nested list to one level list in python"
Dallas
14 Jul 2017
1>>> from collections import Iterable
2>>> def flat(lst):
3...     for parent in lst:
4...         if not isinstance(i, Iterable):
5...             yield parent
6...         else:
7...             for child in flat(parent):
8...                 yield child
9...
10>>> list(flat(([1,[2,2,2],4]))
11[1, 2, 2, 2, 4]