Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading