Evening all, I have recently discover the * operator to unpack my list. I find it quite elegant but I am a bit struggling with it.
Please find below an example :
from matplotlib.pyplot import Line2D
COLOR_FCT = {
"a": ["blue", "Al", "-"],
"b": ["orange", "Bv", "-"],
"c": ["green", "Cx", "-"],
"d": ["k", "Ds", "--"],
}
legend = [
Line2D(
[0],[0],color=COLOR_FCT[item][0],lw=2,ls=COLOR_FCT[item][2],label=COLOR_FCT[item][1],)
for item in ["a", "b", "c"]]
Is there a way to avoid assigning myself the color, ls and label variables using the * operator ? I made a test with : for zip(*list(item)) but I would be grateful for insights or additional documentation. Thanks a lot, Mat
CodePudding user response:
If you changed the dictionary of lists to a dictionary of dictionaries, you can use the similar **
unpacking operator:
COLOR_FCT = {
"a": {"color": "blue", "label": "Al", "ls": "-"],
"b": ["color": "orange", "label": "Bv", "ls": "-"],
]
legend = [
Line2D([0],[0], **COLOR_FCT[item])
for item in ["a", "b", "c"]
]
This unpacks the dictionaries into the argument list.