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
  • 76,709
  • 19
  • 122
  • 186
user9940344
  • 415
  • 1
  • 3
  • 15

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
  • 24,366
  • 6
  • 30
  • 49