I've made a loop that gives me data in the following format:
name_quant = [{'name_id': 'S00004', 'quantity': '1'}, {'name_id': 'S00004', 'quantity': '2'}, {'name_id': 'S00003', 'quantity': '1'},
{'name_id': 'S00003', 'quantity': '2'}, {'name_id': 'S00003', 'quantity': '2'}, {'name_id': 'S00002', 'quantity': '1'}]
I used the following loop to get the values above:
namesequence = EventSequence.objects.filter(description="names").values("Details")
name_quant = [{ 'name_id': e['element'][33:39],
'quantity': e['element'][50:51] } for e in namesequence ]
So my question is how can I aggregate the name_ids and sum the quantities of matching name_ids so that i get a result like so:
name_sum = [{'name_id': 'S00001', 'quantity': '160'}, {'name_id': 'S00002', 'quantity': '50'}, {'name_id': 'S00003', 'quantity': '40'}, {'name_id': 'S00004', 'quantity': '90'}]
I would have used the sum function in Django but I have to subscript and loop though the value first which makes it a bit more complicated.