string to list when the contents is a list

W

Wes James

I have been trying to create a list form a string. The string will be
a list (this is the contents will look like a list). i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return []

But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].

How can I do this?

thx,

-wes
 
N

nn

Wes said:
I have been trying to create a list form a string. The string will be
a list (this is the contents will look like a list). i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return []

But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].

How can I do this?

thx,

-wes


I am surprised nobody gave you the simple answer yet that may even
work for your situation:

b=a[2:-2].split("','")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top