1

Hi I have the following string:

s = '{'abc', 'def'}'

I want to create a list

L = ['abc', 'def']

How is best to achieve this?

I can do the standard replace etc and create a list but keeping in mind the length + # of elements of s will change.

Mad Physicist
  • 95,415
  • 23
  • 151
  • 231
user9940344
  • 504
  • 1
  • 5
  • 20

1 Answers1

5

If you have situation such as this one:

s = "{'abc', 'def'}"

Use literal_eval :

from ast import literal_eval
list(literal_eval(s))
['abc', 'def']
zipa
  • 26,044
  • 6
  • 38
  • 55