How to convert a string into a list in Python

A string can be converted into a list if the string contains a list expression. For example, the string ‘[“a”, “b”, “c”]’ can be converted to a list.

Solution for How to convert a string into a list in Python : You can use ast.literal_eval() to convert a string into a list Call ast.literal_eval(node_or_string) with a string as node_or_string to convert it to a list.

Other Solutions

Use json.loads() to convert a string to a list

  • This solution is safer and faster, but it requires the string to be wrapped in single quotes and the items in the list in double quotes.
  • Further Reading: See documentation for json.loads().


how-to-convert-a-string-into-a-list-in-python