Hello How can I convert string '[1,2,3,4]' to list in python having the following p>
valor_inicial = str([1,2,3])
Now I want that initial_value to convert it into a list. How can I do it?
Hello How can I convert string '[1,2,3,4]' to list in python having the following p>
valor_inicial = str([1,2,3])
Now I want that initial_value to convert it into a list. How can I do it?
I already managed to find 2 alternatives:
first:
valor_inicial = str([1,2,3])
import json
json.loads(valor_inicial)
second:
from ast import literal_eval
valor_inicial = str([1,2,3])
literal_eval(valor_inicial)
print(map(int, "[1,2,3]".replace('[', '').replace(']', '').split(',')))
"["
and "]"
split()
for the ,
to get a list int
, we do it with map(lista, int)