How to subtract values from dictionaries

Just an update to Haidro answer.

Recommended to use subtract method instead of "-".

d1.subtract(d2)

When - is used, only positive counters are updated into dictionary. See examples below

c = Counter(a=4, b=2, c=0, d=-2)
d = Counter(a=1, b=2, c=3, d=4)
a = c-d
print(a)        # --> Counter({'a': 3})
c.subtract(d)
print(c)        # --> Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})

Please note the dictionary is updated when subtract method is used.

And finally use dict(c) to get Dictionary from Counter object


I think a very Pythonic way would be using dict comprehension:

d3 = {key: d1[key] - d2.get(key, 0) for key in d1}

Note that this only works in Python 2.7+ or 3.


Use collections.Counter, iif all resulting values are known to be strictly positive. The syntax is very easy:

>>> from collections import Counter
>>> d1 = Counter({'a': 10, 'b': 9, 'c': 8, 'd': 7})
>>> d2 = Counter({'a': 1, 'b': 2, 'c': 3, 'e': 2})
>>> d3 = d1 - d2
>>> print d3
Counter({'a': 9, 'b': 7, 'd': 7, 'c': 5})

Mind, if not all values are known to remain strictly positive:

  • elements with values that become zero will be omitted in the result
  • elements with values that become negative will be missing, or replaced with wrong values. E.g., print(d2-d1) can yield Counter({'e': 2}).

Haidro posted an easy solution, but even without collections you only need one loop:

d1 = {'a': 10, 'b': 9, 'c': 8, 'd': 7}
d2 = {'a': 1, 'b': 2, 'c': 3, 'e': 2}
d3 = {}

for k, v in d1.items():
    d3[k] = v - d2.get(k, 0) # returns value if k exists in d2, otherwise 0

print(d3) # {'c': 5, 'b': 7, 'a': 9, 'd': 7}