I am doing a very simple computation, where I find identical key/value pairs in a list of dicts to combine them by summing them up. Lets say the data is:
Edit: name & id are arbitrary names for example, I have a very large dict where I use multiple keys
Input
{
"name":"first",
"id":"1234",
"quantity":10
},
{
"name":"first",
"id":"1234",
"quantity":30
},
{
"name":"another",
"id":"0000",
"quantity":10
}
Output
{
"name":"first",
"id":"1234",
"quantity":40
},
{
"name":"another",
"id":"0000",
"quantity":10
}
I am curious to learn on how to do this the "pythonic" way, avoiding nested loops if possible.
For now I have it as such which I am not happy with:
for entry in quantities:
for compare in quantities:
if id(entry) != id(compare):
if (entry["name"] == compare["name"]) and (entry["id"] == compare["id"]):
entry["quantity"] = entry["quantity"] + compare["quantity"]
quantities.remove(compare)
Any tips/suggestions are appreciated, thanks!