W3docs

How to convert string representation of list to a list

You can use the ast.literal_eval() function from the ast module to safely evaluate a string and convert it to a list.

You can use the ast.literal_eval() function from the ast module to safely evaluate a string and convert it to a list. Here's an example:

Convert string representation of list to a list in Python

python— editable, runs on the server
Info

Watch a video course: Python - The Practical Guide

This method is safe for untrusted input because ast.literal_eval() only parses Python literals and does not execute arbitrary code.