I have a string that is
string_list = '[15,30]'
How can I convert it to make it into a list with the values as integers?
Expected Output:
integer_list = [15,30]
I have a string that is
string_list = '[15,30]'
How can I convert it to make it into a list with the values as integers?
Expected Output:
integer_list = [15,30]
Use json
, it will load a python object from a string version of that object .
loads
is for load str
, if I remember correctly.
import json
integer_list = json.loads(string_list)
More info on json
: https://docs.python.org/3/library/json.html
You can use ast.literal_eval
import ast
string_list = '[15,30]'
integer_list = ast.literal_eval(string_list)
print(integer_list)
What about that ?
string_list = '[15,30]'
string_list = string_list[1:]
string_list = string_list[:-1]
integer_list = string_list.split(",")
for item in integer_list :
item = int(item)