itertools - How to flatten a list of lists of lists in python -


this question has answer here:

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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -