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. Here's an example:
import ast
string_representation = "[1, 2, 3, 4, 5]"
list_from_string = ast.literal_eval(string_representation)
print(list_from_string) # Output: [1, 2, 3, 4, 5]
Watch a video course
Python - The Practical Guide
Please be careful when using this method with untrusted input as it can be unsafe, as it will evaluate any python expressions passed to it.