string to list? possible?

brueckd at tbye.com brueckd at tbye.com
Sun Jan 20 14:50:50 EST 2002


On Sun, 20 Jan 2002, Mark McEahern wrote:

> maximilianscherr wrote:
> > i need to convert a string like "[1, 2, 3, 4]" to a list [1, 2, 3, 4].
> > possible?
>
> There are probably better ways than the following, but it works:
>
> $ python
> Python 2.2 (#1, Dec 31 2001, 15:21:18)
> [GCC 2.95.3-5 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> s = "[1, 2, 3, 4]"
> >>> s = s.replace("[", "")
[snip]

Or just use eval:

>>> eval("[1, 2, 3, 4]")
[1, 2, 3, 4]

-Dave





More information about the Python-list mailing list