Avii Avii - 23 days ago 5
Python Question

How to split a string inside a list in Python?

How can I convert this:

['3 5 20']


to this:

['3', '5', '20']

Answer

Use str.split() with a list comprehension:

>>> L = ['3 5 20']
>>> [x for s in L for x in s.split()]
['3', '5', '20']

This will work for lists containing one or more strings.