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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.