2

I have this string:

num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"

and I want to return/output:

[['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']]

how do i get to this list of lists from that huge string?

2 Answers2

4

ast.literal_eval is a good for exactly that.

>>> num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
>>> import ast
>>> list(ast.literal_eval(num))
[['1', '9', '7', '6'], ['2', '0', '8', '3', '7'], ['3', '8', '5', '7', '9', '10', '4']]
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
1

You can use AST:

import ast
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
num = list(ast.literal_eval(num))
Henry Zhu
  • 2,488
  • 9
  • 43
  • 87