Alex_rdq
Alex_rdq

Reputation: 39

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

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?

Upvotes: 3

Views: 71

Answers (1)

Cardstdani
Cardstdani

Reputation: 5223

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]]

Upvotes: 2

Related Questions