Like this:
>>> text = 'a,b,c'
>>> text = text.split(',')
>>> text
[ 'a', 'b', 'c' ]
Answer from Cameron on Stack OverflowLike this:
>>> text = 'a,b,c'
>>> text = text.split(',')
>>> text
[ 'a', 'b', 'c' ]
Just to add on to the existing answers: hopefully, you'll encounter something more like this in the future:
>>> word = 'abc'
>>> L = list(word)
>>> L
['a', 'b', 'c']
>>> ''.join(L)
'abc'
But what you're dealing with right now, go with @Cameron's answer.
>>> word = 'a,b,c'
>>> L = word.split(',')
>>> L
['a', 'b', 'c']
>>> ','.join(L)
'a,b,c'
Videos
I'm not sure that this is the fastest, but it's definitely the safest/easiest:
import ast
lst = ast.literal_eval(s)
regular eval would work too:
lst = eval(s)
Some basic timings from my machine:
>>> s = '[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]'
>>> def f1():
... eval(s)
...
>>> def f2():
... ast.literal_eval(s)
...
>>> timeit.timeit('f1()', 'from __main__ import f1')
31.415852785110474
>>> timeit.timeit('f2()', 'from __main__ import f2')
46.25958704948425
So, according to my computer, eval is about 50% faster than ast.literal_eval. However, eval is terribly unsafe and should never be used on any string unless you trust it completely. Unless this is a real demonstratable bottleneck and you trust the input 100%, I would consider the little bit of extra time worth it in exchange for being able to sleep soundly at night.
Since we care about speed, in this particular case I might use json.loads:
>>> import ast, json
>>> s = "[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]"
>>> %timeit ast.literal_eval(s)
10000 loops, best of 3: 61.6 ยตs per loop
>>> %timeit eval(s)
10000 loops, best of 3: 45.7 ยตs per loop
>>> %timeit json.loads(s)
100000 loops, best of 3: 6.61 ยตs per loop
>>> json.loads(s)
[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]
(Note that this works here because this line is sufficiently JSON-like. It can't be used everywhere that ast.literal_eval can because not all Python literal syntax is valid JSON.)
You can use literal_eval in the ast module
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
If you can convert those single quotes to double quotes, you can use json parsing.
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')