1

How to parse string to list in Python ?

myStringValue = "[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]"

How to convert myStringValue to list ?

1
  • 4
    ast.literal_eval(..) Commented Apr 12, 2017 at 15:09

1 Answer 1

2

Be careful when using it as it's open to exploitation, but eval() is one way forward:

>>> myStringValue = "[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]"
>>> eval(myStringValue)
[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]
>>> type(eval(myStringValue))
<type 'list'>
Sign up to request clarification or add additional context in comments.

1 Comment

json or ast modules would be better here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.