-2

I have a string such as thislist = "[3, 2, 1]" or "[1]"

I need to convert it to a list with float (or int) elements, but i don't know how to do it?

What i tried is this:

thislist = list(thislist)

But, when i try this solution, it gives me:

 ['[', '3', ',', ' ', '2', ',', ' ', '1', ']']

which is not what i need. Of course, i can do some more process to obtain my favorite result which is [3,2,1]. But, i am looking for the most efficient approach!

Jeff
  • 7,767
  • 28
  • 85
  • 138
  • 1
    More accurate dup (list of ints): https://stackoverflow.com/questions/44672561/convert-string-representation-of-list-into-list – Tomerikoo May 31 '20 at 10:13

1 Answers1

2
import json
thislist = "[3, 2, 1]"
goodList = json.loads(thislist) # this will give you [3, 2, 1]
Alok
  • 7,734
  • 8
  • 55
  • 100