-1

I need to add two hash tables in python. However, "+" does not do that:

d1={1:1}
d2={1:2,2:5}
d3=d1+d2

I need d3={1:3,2:5}. I appreciate any suggestion.

EbrahimA
  • 59
  • 1
  • 1
  • 6

1 Answers1

2

Use collections.Counter:

>>> from collections import Counter
>>> Counter(d1) + Counter(d2)
Counter({2: 5, 1: 3})
simonzack
  • 19,729
  • 13
  • 73
  • 118