Could somebody explain me why i can't get the sum of second index(incoming). I have tried to print the value of variable group after than first iteration it seems that once it has been iterated there are no more values.
I took some code from this example. example post
products = {}
products['product']= [ {
'id':1234,
'stock_id':1001,
'lot_id':5001,
'name':'product1',
'qty':50,
'incoming':100,
}
.................
.................
]
grouper = itemgetter("id","stock_id","lot_id")
result = []
for key, group in groupby(sorted(products['product'], key= grouper),grouper):
temp_dict= dict(zip(["id","stock_id","lot_id"], key))
temp_dict["qty"] = sum(item["qty"] for item in group)
temp_dict["incoming"] = sum(item["incoming"] for item in group)
result.append(temp_dict)
for r in result:
print r
result
{'lot_id': 5001, 'stock_id': 1001, 'incoming': 0, 'id': 1234, 'qty': 250}
{'lot_id': 5001, 'stock_id': 1001, 'incoming': 0, 'id': 1235, 'qty': 50}
{'lot_id': 5002, 'stock_id': 1001, 'incoming': 0, 'id': 1235, 'qty': 100}
{'lot_id': 5001, 'stock_id': 1002, 'incoming': 0, 'id': 1236, 'qty': 100}