'Is there any Pythonic way to manipulate Dictionaries to follow DRY philosophy in Django?

I have got dictionary set of required queries as shown below:

[
    {
        "primary_attribute__name": "Color"
        "primary_attr_values__name": "Red",
    },
    {
        "primary_attribute__name": "Color",
        "primary_attr_values__name": "Green",
    },
]

Now I want :

[
    {
        "primary_attribute__name": "Color"
        {
            "primary_attr_values__name": "Red",
            "primary_attr_values__name": "Green",
            "primary_attr_values__name": "Yellow",
        },
    }
]

Or is this good approach:

primary_attribute = {
    "Color": {
        "primary_attr_values__name": [
            "red",
            "green",
            "yellow",
        ]
    }
}

How it can be achieved?

Edit: Actually I have Product, Product has Variant and each Variant may have VariantAttributes. VariantAttributes has primary_attr, primary_aatr_value ( these are like color: green, color:red, color:green)

For that Product; ProductVariant's ProductAttribute primary_attr_name will be same, for example : Color, but the values will be different on each variant. I am trying to solve this condition with dictionary.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source