Alex Rivera | Logout

converting a string to a list of tuples

Asked 2011-12-13T14:35:52.083
9

I need to convert a string, '(2,3,4),(1,6,7)' into a list of tuples [(2,3,4),(1,6,7)] in Python. I was thinking to split up at every ',' and then use a for loop and append each tuple to an empty list. But I am not quite sure how to do it. A hint, anyone?

Edit
Report

1 Answer

20
>>> list(ast.literal_eval('(2,3,4),(1,6,7)'))
[(2, 3, 4), (1, 6, 7)]
answered 2011-12-13T14:37:05.343

Your Answer