Convert a string of a list to a list

reference:

solution:

  • use json.loads(your_data) function in the json module to parse a stringified list of dictionaries
  • NOTE: it works for '["a","b"]' but not for "['a','b']"
import json
x = '[ "A","B","C" , " D"]'

json.loads(x)
['A', 'B', 'C', ' D']
x = '["a","b"]' 
json.loads(x)    
['a', 'b']
try:
    x = "['a','b']"
    json.loads(x) 
except:
    print('error')
error