Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #convert #level #nested #list #level #list #python
ADD COMMENT
Topic
Name
3+4 =