'Get a unique list from list in python

I'm struggling with this task. I have a list1 below. I want to get the unique taxes (gst, pst,etc.) and get the total for each one.

list1 = [{
    "taxes": [{
        "amount": 1393,
        "rate": {
            "object": "tax_rate",
            "country": "CA",
            "created": 1643646734,
            "display_name": "PST",
            "percentage": 7.0,
            "tax_type": "pst"
        }
    }, {
        "amount": 995,
        "rate": {
            "object": "tax_rate",
            "country": "CA",
            "created": 1643646734,
            "display_name": "GST",
            "percentage": 5.0,
            "tax_type": "gst"
        }
    }, {
        "amount": 205,
        "rate": {
            "object": "tax_rate",
            "country": "CA",
            "created": 1643646323,
            "display_name": "Special",
            "percentage": 4.0,
            "tax_type": "special"
        }
    }]
}, {
    "taxes": [{
        "amount": 2000,
        "rate": {
            "object": "tax_rate",
            "country": "CA",
            "created": 1643646734,
            "display_name": "PST",
            "percentage": 7.0,
            "tax_type": "pst"
        }
    }, {
        "amount": 1000,
        "rate": {
            "object": "tax_rate",
            "country": "CA",
            "created": 1643646734,
            "display_name": "GST",
            "percentage": 5.0,
            "tax_type": "gst"
        }
    }]
}]

How can I get the list2 below? This one has all unique taxes, then get the total amount for each one. Also, I just need a few values from the list1.

list2 = [
    {
        "tax_name": 'GST',
        "tax_rate": 5.0,
        "tax_amount": 1995
    },
    {
        "tax_name": 'PST',
        "tax_rate": 7.0,
        "tax_amount": 3393
    },
    {
        "tax_name": 'Special',
        "tax_rate": 4.0,
        "tax_amount": 205
    },
]

Thank you very much



Solution 1:[1]

A possible solution is the following code:

def extract(data):
    result = {}
    for element in data:
        for tax in element["taxes"]:
            name = tax["rate"]["tax_type"].upper()
            rate = tax["rate"]["percentage"]
            amount = tax["amount"]
            if name in result:
                result[name]["tax_amount"] += amount
            else:
                result[name] = {
                    "tax_name": name,
                    "tax_rate": rate,
                    "tax_amount": amount
                }
    return list(result.values())
extract(list1)

The idea is to have a dictionary indexes by the name of your tax. If the name is already present in the keys of the dictionary, we just update the total amount by adding the amount of the current tax. If the name is not present in the keys of the dictionary, then we create a new entry with the name, the rate and the current amount.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Raida