"10, 20, 30" to [10, 20, 30]

John Machin sjmachin at lexicon.net
Thu Nov 23 12:25:38 CET 2006


Daniel Austria wrote:
> Sorry,
>
> how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
>
> what i can do is:
>
> s = "10, 20, 30"
> tmp = '[' + s + ']'
> l = eval(tmp)
>
> but in my opinion this is not a nice solution

Most people share your opinion. Try this:

| >>> strg = "10, 20, 30"
| >>> [int(x) for x in strg.split(',')]
| [10, 20, 30]

Cheers,
John




More information about the Python-list mailing list