Search
 
SCRIPT & CODE EXAMPLE
 

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]
Comment

PREVIOUS NEXT
Code Example
Python :: check if an object has an attribute in Python 
Python :: two groupby pandas 
Python :: find max number in list python 
Python :: ValueError: cannot convert float NaN to integer 
Python :: new column with addition of other columns 
Python :: make tkinter text editing disabled 
Python :: urllib.request.urlopen with headers 
Python :: remove all elements from list python by value 
Python :: Check if file already existing 
Python :: hex to string python 
Python :: how to convert list to all uppercase 
Python :: similarity index in python 
Python :: Python list files only in given directory 
Python :: String search from multiple files 
Python :: python sqrt function 
Python :: bar plot python 
Python :: chrome webdrivermanager 
Python :: default values python 
Python :: Character limit python system 
Python :: installing python3.9 on linux mint 20 
Python :: download unsplash images python no api 
Python :: initialize np array 
Python :: python built in functions 
Python :: pandas to python datetime 
Python :: django login 
Python :: read csv python 
Python :: push in python 
Python :: how to use django-rest-framework-datatables 
Python :: how to make a window python 
Python :: how to convert response to beautifulsoup object 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =