0

I read from stdin the following type of strings in Python:

"[['A', '0', '12.0'], ['A', '1', '10.0'], ['B', '1', '10.0']]"

How can I turn them into a list of lists?

4
  • 3
    That is a list of lists! Commented Mar 7, 2016 at 10:29
  • 1
    Congratulations, you did it already. Commented Mar 7, 2016 at 10:29
  • My bad. I changed the post. Those are strings that I read and I want to turn them into lists of lists Commented Mar 7, 2016 at 10:31
  • ...that's still a list of lists of strings. Please give a minimal reproducible example that actually reflects the problem you're trying to solve. Commented Mar 7, 2016 at 10:32

2 Answers 2

7

If you mean you have that in a string, use ast.literal_eval():

>>> s = "[['A', '0', '12.0'], ['A', '1', '10.0'], ['B', '1', '10.0']]"
>>> import ast
>>> ast.literal_eval(s)
[['A', '0', '12.0'], ['A', '1', '10.0'], ['B', '1', '10.0']]
Sign up to request clarification or add additional context in comments.

Comments

0
new_list = eval("[['A', '0', '12.0'], ['A', '1', '10.0'], ['B', '1', '10.0']]")

Comments

Your Answer

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.