How to turn a string into a list in python?

list, python, string

Solution

the `ast` module has a `literal_eval` that does what you want

import ast
l = "['Hello', 'my', 'name', 'is', 'Apple']"
l1 = ast.literal_eval(l)

Outputs:

['Hello', 'my', 'name', 'is', 'Apple']

docs

Problem

``` l = "['Hello', 'my', 'name', 'is', 'Apple']" l1 = ['Hello', 'my', 'name', 'is', 'Apple'] ``` `type(l)` returns `str` but I want it to be a list, as `l1` is. How can I transform that string into a common list?

Original source