0

I have 2 dictionaries with identical keys.

d1 = {'Dog':[7,2],'Cat':[5,2]}
d2 = {'Dog':1,'Cat':4}

Is there a good way of combining them so that I can have one dictionary that looks like this?

d = {'Dog':[7,2,1],'Cat':[5,2,4]}
user2333196
  • 5,406
  • 7
  • 31
  • 35
  • The `duplicate` is not an answer to this question. It should be reopened or given a correct duplicate. – hpaulj Feb 27 '16 at 15:57

2 Answers2

2
for key, value in d2.iteritems():
    if key in d1:
        d1[key].append(value)
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
2

If one contains lists and the other contains ints, you can do:

d = {key:[d2[key]] + d1[key] for key in d1}
GLaDOS
  • 620
  • 6
  • 17