-2

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']

SH_IQ
  • 455
  • 1
  • 10

2 Answers2

2
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

E. Gertz
  • 221
  • 3
  • 12
1

Does this help?

>>> color ="['Red', 'Green', 'White']"
>>> color = [x.strip(" '") for x in color.strip('[]').split(',')]
>>> color
['Red', 'Green', 'White']
mshsayem
  • 15,979
  • 11
  • 57
  • 65