-3

I have a string like "['apple' 'bat' 'cat']".
The string need to convert into array like:['apple','bat','cat']

0stone0
  • 34,288
  • 4
  • 39
  • 64
Anonymous
  • 111
  • 6

1 Answers1

1

remove the first [ and last ] elements of your string

then split the remaining string into their elements

Iterate then each element and remove the opening and closing quote (first and last element)

Merge everything in a list comprehension

my_string = "['apple' 'bat' 'cat']"
result = [i[1:-1] for i in my_string[1:-1].split(' ')]
print(result)

['apple', 'bat', 'cat']
Sembei Norimaki
  • 745
  • 1
  • 4
  • 11