1

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}
Community
  • 1
  • 1
kamboj
  • 411
  • 1
  • 9
  • 18

1 Answers1

3

You consume group iterator in the first sum, call list on group group = list(group) to store the contents in a list so you can use them twice:

for key, group in groupby(sorted(products['product'], key=grouper), grouper):
    temp_dict = dict(zip(["id", "stock_id", "lot_id"], key))
    group = list(group)
    temp_dict["qty"] = sum(item["qty"] for item in group)
    temp_dict["incoming"] = sum(item["incoming"] for item in group)

You are basically doing:

In [4]: group = iter([1,2,3,4])

In [5]: for ele in group: # iterator will be consumed
           print(ele)
   ...:     
1
2
3
4

In [6]: for ele in group: # nothing left to iterate
           print(ele)
   ...:     

In [7]: 
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321