itertools - How to flatten a list of lists of lists in python -
this question has answer here:
- flatten (an irregular) list of lists 36 answers
i've seen couple answers on how flatten lists of form
[1,[1,2],[3]]     print list(itertools.chain(*[1,[1,2],[3]]))   but how flatten lists this:
[[1],[[1,2],[3]]]  print list(itertools.chain(*[[1],[[1,2],[3]]])) [1, [1, 2], [3]] 
i use recipe:
import collections   def flatten(l):      el in l:         if isinstance(el, collections.iterable) , not isinstance(el, str):             sub in flatten(el):                 yield sub         else:             yield el   print(list(flatten([[1],[[1,2],[3]]]))) # [1, 1, 2, 3] 
Comments
Post a Comment