-2

Suppose I have two dictionaries

a = {'milk':90, 'coffee':80, 'rice':100, 'Cheese': 70}

b = {'milk':90, 'coffee':80, 'pulses': 100,'Alcohol':750}

I want to merge these two dictionaries by adding value of common elements of the dictionaries which should give resultant as:

c = {'milk':180, 'coffee':160, 'rice':100, 'Cheese':70, 'pulses':100, 'Alcohol':750}

I am trying this at my level , please suggest if anyone have a solution for this.

martineau
  • 119,623
  • 25
  • 170
  • 301
Ripundeep Gill
  • 231
  • 5
  • 18

2 Answers2

4

Use the keys of either dicts and add their value to make the third, so it doesn't matter which items are unique to just one of them. Use the dict.get to fetch the value for each key from both dicts, defaulting to 0 if that item is not present (since we're adding).

>>> c = {}
>>> for key in set(a.keys() + b.keys()):
...     c[key] = a.get(key, 0) + b.get(key, 0)
...
>>> c
{'Cheese': 70, 'coffee': 160, 'Alcohol': 750, 'pulses': 100, 'rice': 100, 'milk': 180}

And a one-liner version of that, if you prefer:

>>> d = {key: a.get(key, 0) + b.get(key, 0) for key in set(a.keys() + b.keys())}
>>> d == c
True
>>>
aneroid
  • 12,983
  • 3
  • 36
  • 66
  • Nice one. I'm not sure whose solution I like better! :) – Jon Kiparsky Jun 18 '16 at 05:49
  • And my answer appears to be [a close dupe of one of the answers](http://stackoverflow.com/a/11011911/1431750) on the dupe question :-/ – aneroid Jun 18 '16 at 05:56
  • 1
    Indicating that it's in canonical style. (or, in the annoying idiom, "pythonic") – Jon Kiparsky Jun 18 '16 at 06:05
  • Hello Aneroid, First, I would like to thankyou for such nice source code to solve this problem. I found the one liner code is efficient and faster solution. it is a huge help. Once again thankyou. It worked for me. Also I came up with my solution as well which I am posting in my answer. Please do check. :) Huge Thanks & regards, Ripundeep Gill – Ripundeep Gill Jun 18 '16 at 06:38
  • You're welcome. And welcome to SO. You won't be able to add your answer/solution since this thread has been closed - as being a [duplicate of this one](http://stackoverflow.com/q/11011756/1431750). But if you want to share what you have done/tried or worked for you, you can add it as an Edit to your original question above. – aneroid Jun 18 '16 at 06:42
  • 1
    a = {'milk':90, 'coffee':80, 'rice':100, 'Cheese': 70}, b = {'milk':90, 'coffee':80, 'pulses': 100,'Alcohol':750}, from collections import Counter a = Counter(a) b = Counter(b) c = a+b c = dict(c) print c **output: {'Cheese': 70, 'coffee': 160, 'pulses': 100, 'Alcohol': 750, 'rice': 100, 'milk': 180}** – Ripundeep Gill Jun 18 '16 at 06:45
2

You could try something like this:

>>> def merge_dicts(a,b):
...   c = {}
...   for k, v in a.items():
...     c[k] = a[k] + b.get(k, 0)
...   b.update(c)
...   return b
... 
>>> merge_dicts(a,b)
{'Cheese': 70, 'coffee': 160, 'Alcohol': 750, 'pulses': 100, 'rice': 100, 'milk': 180}
>>> 

NOTE: Please feel free to ask if there's anything you don't understand here - it's no good just learning incantations if you're not clear on why they work!

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • Note that you're you're creating a new dict `c` but then updating the existing dict `b` and returning that one. ie. `b` gets modified when calling `merge_dicts`. – aneroid Jun 18 '16 at 05:51
  • Hello Jon, First, I would like to thank you for such nice source code to solve this problem. Making a function is one way if we need to generalize something with parameters and reusing it within code. I appreciate the way you did it. it is a huge help. Once again thankyou. It worked for me. Also I came up with my solution as well which I am posting in my answer. Please do check. :) Huge Thanks & regards, Ripundeep Gill – Ripundeep Gill Jun 18 '16 at 06:41
  • a = {'milk':90, 'coffee':80, 'rice':100, 'Cheese': 70}, b = {'milk':90, 'coffee':80, 'pulses': 100,'Alcohol':750}, from collections import Counter a = Counter(a) b = Counter(b) c = a+b c = dict(c) print c output: {'Cheese': 70, 'coffee': 160, 'pulses': 100, 'Alcohol': 750, 'rice': 100, 'milk': 180} – Ripundeep Gill Jun 18 '16 at 06:46