Python function which can transverse a nested list and print out each element -


this question has answer here:

for example if have list [[['a', 'b', 'c'], ['d']],[['e'], ['f'], ['g']]] function should print out 'a' 'b' 'c' ...ect. on separate lines.it needs able variation in list.

my_list = [[['a', 'b', 'c'], ['d']],[[[['e', ['f']]]]], ['g']] def input1(my_list):          in range(0, len(my_list)):             my_list2 = my_list[i]             in range(0, len(my_list2)):                 print(my_list2[i]) 

my code doesnt seem work, know missing alot of whats needed neccesary function. suggestions helpful

you'll want first flatten list:

>>> import collections >>> def flatten(l): ...     el in l: ...         if isinstance(el, collections.iterable) , not isinstance(el, basestring): ...             sub in flatten(el): ...                 yield sub ...         else: ...             yield el ...  >>> l = [[['a', 'b', 'c'], ['d']],[['e'], ['f'], ['g']]] >>> item in flatten(l): ...     print item ...  b c d e f g 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -