Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining a list from a string

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
0
0
CA
Hi,

I have a list, which I get with quotations enclosing it, hence making it a string. So what I get is: '[123, 456]'. My question is that how can I convert this into an actual list. So basically after performing some operation, obtain a list [123, 456] out.
 
A quick way if you are sure your input is formatted like a list is to use "eval":
Code:
s = '[123, 456]'
new_list = eval(s, {'__builtins__': {}}, {})
if isinstance(new_list, list):
  for item in new_list:
    print item

But be aware that using "eval" evaluates arbitrary expressions contained in s, so if you open yourself up to dangerous things if you don't at least call it with empty local and global namespaces.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top