Colargol
Colargol

Reputation: 769

How to subtract values from dictionaries

I have two dictionaries in Python:

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

I want to substract values between dictionaries d1-d2 and get the result:

d3 = {'a': 9, 'b': 7, 'c': 5, 'd': 7 }

Now I'm using two loops but this solution is not too fast

for x,i in enumerate(d2.keys()):
        for y,j in enumerate(d1.keys()):

Upvotes: 26

Views: 51161

Answers (4)

TerryA
TerryA

Reputation: 59974

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}).

Upvotes: 25

Erfa
Erfa

Reputation: 713

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.

Upvotes: 41

Hemanth
Hemanth

Reputation: 315

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

Upvotes: 14

joente
joente

Reputation: 856

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}

Upvotes: 4

Related Questions