String To List

Dan Bishop danb_83 at yahoo.com
Mon Mar 17 02:56:50 EDT 2008


On Mar 17, 1:15 am, Girish <girish.... at gmail.com> wrote:
> I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> list with elements 'xyz' and 'abc'. Is there any simple solution for
> this??
> Thanks for the help...

eval(a) will do the job, but you have to be very careful about using
that function.  An alternative is

[s.strip('\'"') for s in a.strip('[]').split(', ')]



More information about the Python-list mailing list