I have a list of dictionaries like this`
[
{
"account_name": "Rounded Off (Purchase)",
"amount": 0.28,
"doc_date": "2023-04-05",
"doc": "P.Inv.-1",
"date_created": "2023-04-05T15:30:42.964203"
},
{
"account_name": "Discount (Purchase)",
"amount": 100,
"doc_date": "2023-04-05",
"doc": "P.Inv.-1",
"date_created": "2023-04-05T15:30:42.964203"
},
{
"account_name": "Discount (Purchase)",
"amount": 86.4,
"doc_date": "2023-04-05",
"doc": "P.Inv.-1",
"date_created": "2023-04-05T15:30:42.964203"
}
]`
I would like to simplify the list by adding the "amount" if two dictionaries have the same values for "doc" and "account_name" keys e.g. in this case "account_name" : "Discount (Purchase)" and "doc" : "P.Inv-1".
I can not think of a simple solution without using a lots of placeholder variables and and multiple loops over the list.
The expected result should look like
[
{
"account_name": "Rounded Off (Purchase)",
"amount": 0.28,
"doc_date": "2023-04-05",
"doc": "P.Inv.-1",
"date_created": "2023-04-05T15:30:42.964203"
},
{
"account_name": "Discount (Purchase)",
"amount": 186.4,
"doc_date": "2023-04-05",
"doc": "P.Inv.-1",
"date_created": "2023-04-05T15:30:42.964203"
}
]
Any help is greatly appreciated. Thanks.