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
  • 107,652
  • 25
  • 181
  • 264
user9940344
  • 574
  • 1
  • 8
  • 26

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
  • 27,316
  • 6
  • 40
  • 58