Reputation: 117
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?
Upvotes: 0
Views: 68
Reputation: 13750
new_list = eval("[['A', '0', '12.0'], ['A', '1', '10.0'], ['B', '1', '10.0']]")
Upvotes: 0
Reputation: 95652
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']]
Upvotes: 7