0

string type

a = " ['a','b','c']"

expected list:

a
> ['a','b','c']

How would I convert a list form in string to a true list?

Chipmunkafy
  • 486
  • 1
  • 5
  • 15
  • 2
    [see this answer](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – e.doroskevic Nov 24 '18 at 00:01
  • @slider: interestingly, I get an error for the OP's input. `ast.literal_eval` does not like that space in front at all: `IndentationError: unexpected indent` – Jongware Nov 24 '18 at 00:03
  • 1
    @usr2564301 You're right, I see that too. `strip` seems like a good simple fix. – slider Nov 24 '18 at 00:24

1 Answers1

1

give the below a try:

eval(a.strip(' '))

EDIT:

eval is evil. Please see the link provided by e.doroskevic !!

pangyuteng
  • 1,547
  • 12
  • 26