Eval list of strings python

There isn't any need to import anything or to evaluate. You can do this in one line for most basic use cases, including the one given in the original question.

One liner

l_x = [i.strip[] for i in x[1:-1].replace['"',""].split[',']]

Explanation

x = '[ "A","B","C" , " D"]'
# String indexing to eliminate the brackets.
# Replace, as split will otherwise retain the quotes in the returned list
# Split to convert to a list
l_x = x[1:-1].replace['"',""].split[',']

Outputs:

for i in range[0, len[l_x]]:
    print[l_x[i]]
# vvvv output vvvvv
'''
 A
B
C
  D
'''
print[type[l_x]] # out: class 'list'
print[len[l_x]] # out: 4

You can parse and clean up this list as needed using list comprehension.

l_x = [i.strip[] for i in l_x] # list comprehension to clean up
for i in range[0, len[l_x]]:
    print[l_x[i]]
# vvvvv output vvvvv
'''
A
B
C
D
'''

Nested lists

If you have nested lists, it does get a bit more annoying. Without using regex [which would simplify the replace], and assuming you want to return a flattened list [and the zen of python says flat is better than nested]:

x = '[ "A","B","C" , " D", ["E","F","G"]]'
l_x = x[1:-1].split[',']
l_x = [i
    .replace[']', '']
    .replace['[', '']
    .replace['"', '']
    .strip[] for i in l_x
]
# returns ['A', 'B', 'C', 'D', 'E', 'F', 'G']

If you need to retain the nested list it gets a bit uglier, but it can still be done just with regular expressions and list comprehension:

import re

x = '[ "A","B","C" , " D", "["E","F","G"]","Z", "Y", "["H","I","J"]", "K", "L"]'
# Clean it up so the regular expression is simpler
x = x.replace['"', ''].replace[' ', '']
# Look ahead for the bracketed text that signifies nested list
l_x = re.split[r',[?=\[[A-Za-z0-9\',]+\]]|[?

Chủ Đề