harry77
harry77

Reputation: 117

How to create a list of lists from a string?

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

Answers (2)

BallpointBen
BallpointBen

Reputation: 13750

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

Upvotes: 0

Duncan
Duncan

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

Related Questions