I need to convert this string color ="['Red', 'Green', 'White']"
to a list without using ready modules such as (import ......).
The expected results: color = ['Red', 'Green', 'White']
I need to convert this string color ="['Red', 'Green', 'White']"
to a list without using ready modules such as (import ......).
The expected results: color = ['Red', 'Green', 'White']
color = color[1:-1].split("'")
list_colors = []
for i in color:
if len(i) > 2:
list_colors.append(i)
print(list_colors)
that should do the job
Does this help?
>>> color ="['Red', 'Green', 'White']"
>>> color = [x.strip(" '") for x in color.strip('[]').split(',')]
>>> color
['Red', 'Green', 'White']