Python List of Strings created from a list of one string -


say have list ['dogs, cats']. how can 1 turn ['dogs', 'cats'] arbitrary number of ['x, y, z']

just split first element on ', ':

>>> ['dogs, cats'][0].split(', ') ['dogs', 'cats'] >>> >>> ['x, y, z'][0].split(', ') ['x', 'y', 'z']  

if can have multiple comma separated string in list, can use list comprehension:

>>> li = ['x, y, z', 'dogs, cats'] >>>  >>> li2 = [elem.split(', ') elem in li] >>> [v val in li2 v in val] ['x', 'y', 'z', 'dogs', 'cats'] 

or using sum() on list comprehension:

>>> li = ['x, y, z', 'dogs, cats'] >>> >>> sum([elem.split(', ') elem in li], []) ['x', 'y', 'z', 'dogs', 'cats'] 

and itertools:

>>> list(itertools.chain.from_iterable(elem.split(', ') elem in li)) ['x', 'y', 'z', 'dogs', 'cats'] 

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 -

Function that returns a formatted array in VBA -