fork download
  1. def combine_dict(d1,d2):
  2. if not d1:
  3. return d2
  4. elif not d2:
  5. return d1
  6. else:
  7. for key in d2:
  8. if key not in d1:
  9. d1[key] = 0
  10. d1[key] += d2[key]
  11. return dict(sorted(d1.items()))
  12. d1 = {'a': 1, 'b': 2}
  13. d2 = {'c': 3, 'b': 4}
  14. print(combine_dict(d1,d2))
  15.  
  16.  
Success #stdin #stdout 0.11s 14092KB
stdin
Standard input is empty
stdout
{'a': 1, 'b': 6, 'c': 3}