How to convert this mixed 3D list string to 3D list with python

Viewed 29

I have this:

l_string = """[[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], ["#e9446a", 1]]"""

and I want:

l = [[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], [1, "#e9446a", 1]]

How can I convert it?

1 Answers

You need to use eval() to parse the string to a valid Python list:

l_string = """[[-0.5, '#572fa5', 1], [-0.25, "#70339b", 1], [0.2, "#883691", 0.3], [0.5, "#b83d7e", 1], ["#e9446a", 1]]"""
l_string = eval(l_string)
print(l_string)

Output:

[[-0.5, '#572fa5', 1], [-0.25, '#70339b', 1], [0.2, '#883691', 0.3], [0.5, '#b83d7e', 1], ['#e9446a', 1]]
Related