How can I convert a string list to a list of ints?

Viewed 1299

Say I have a string: line = "[1, 2, 12]\n"
and I want to convert it to a list of ints: [1, 2, 12]

I have the solution:

new_list = []
for char in line:
    try:
        new_list.append(int(char))
    except ValueError:
        pass

But this doesn't work in the case of numbers with more than one digit. Is there an inbuilt/better way to do this? Thanks

3 Answers
Related