Flatten (an irregular) list of lists in Python

Answer

Using generator functions can make your example a little easier to read and probably boost the performance.

def flatten(l):for el in l:if isinstance(el, collections.Iterable)andnot isinstance(el, basestring):for sub in flatten(el):yield sub
        else:yield el

All python Questions

Ask your interview questions on python

Write Your comment or Questions if you want the answers on python from python Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---