1

Let's say I have this string like this:

string = ("['1.345', '1.346', '1.347']")

So it's formatted like a list, but I need to convert it to an actual list object. How can I do that?

juliomalegria
25k14 gold badges77 silver badges89 bronze badges
asked Feb 27, 2012 at 0:04
1
  • 1
    Where did the list come from? Commented Feb 27, 2012 at 0:06

4 Answers 4

12

You can use literal_eval from the ast module.

>>> string = ("['1.345', '1.346', '1.347']")
>>> import ast
>>> lst = ast.literal_eval(string)
>>> lst
['1.345', '1.346', '1.347']

BTW, you don't need the parentheses around the string. string = "['1.345', '1.346', '1.347']" works just fine.

answered Feb 27, 2012 at 0:05
Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, this is (much, much) better than eval because it only evaluates literals, not actual code. So there isn't a security issue with taking arbitrary user input.
1
string = ("['1.345', '1.346', '1.347']")
lst = eval(string)
answered Feb 27, 2012 at 0:06

2 Comments

Argh, imagine this is user input... What else should it be?
Yes- Praveen's literal_eval is better
1

It might help if we knew where these strings come from. In general, Praveen's answer is a good one regarding the example you give. I'd like to mention, though, that using doublequotes instead of singlequotes in your string would make the whole thing valid JSON, in which case you could also do:

import json
json.loads('["1.345", "1.346", "1.347"]') # [u'1.345', u'1.346', u'1.347']

Measuring the performance:

import timeit
s = """\
import ast
ast.literal_eval("['1.345', '1.346', '1.347']")
"""
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000) / 100000)

Result: 21.25 usec/pass

vs.

import timeit
s = """\
import json
json.loads('["1.345", "1.346", "1.347"]')
"""
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000) / 100000)

Result: 6.32 usec/pass

Maybe this is an alternative for you.

answered Feb 27, 2012 at 1:02

2 Comments

Putting aside the fact that the OP would have to convert the quotes in his strings, why would you think that the json module is less flexible than the ast module in this context?
Yeah, actually I have to agree with you, JSON can represent any data that Python literals can. Removed my comment :)
0

I would strip '[',']'.etc and use split() method on that string..

lst = string.replace('[','').replace(']','').replace("'",'').replace(" ",'').split(',')

I think it's safer than eval if the string is well formatted, but not quite efficient though, because Python has to build a new string with every replace method.

answered Feb 27, 2012 at 0:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.