0

I want to count the value of the key dictionary. I understood Counter in typing module ignore the element with value 0. The code is:

from functools import reduce
from typing import Counter

pen={'age':0, 'cost':10}
bottle={'age':0,'cost':30}
all = [pen, bottle]
reduce(lambda a,b: a+b, (Counter(item) for item in all))

The output is {'cost':40} and Counter ignores the element with count 0. I want the output is {'cost':40, 'age':0}. There are other ways to count the value of keys in the dictionary?

GreenMan
  • 269
  • 1
  • 15

1 Answers1

0

Thanks, Hussain Hassam

Based on This I edit code based on it:

from functools import reduce
from typing import Counter

pen={'age':0, 'cost':10}
bottle={'age':0,'cost':30}
all = [pen, bottle]
reduce(lambda a, b: a.update(b) or a, all, Counter())
GreenMan
  • 269
  • 1
  • 15