I have a string.
s = '1989, 1990'
I want to convert that to list using python & i want output as,
s = ['1989', '1990']
Is there any fastest one liner way for the same?
I have a string.
s = '1989, 1990'
I want to convert that to list using python & i want output as,
s = ['1989', '1990']
Is there any fastest one liner way for the same?
Use list comprehensions:
s = '1989, 1990'
[x.strip() for x in s.split(',')]
Short and easy.
Additionally, this has been asked many times!
Use the split method:
>>> '1989, 1990'.split(', ')
['1989', '1990']
But you might want to:
remove spaces using replace
split by ','
As such:
>>> '1989, 1990,1991'.replace(' ', '').split(',')
['1989', '1990', '1991']
This will work better if your string comes from user input, as the user may forget to hit space after a comma.