title: How can I merge and sum two dictionary key and items?
body: <p>I have two dictionarys</p>
  
  <pre><code>dict1 = {
  "list": {
      "alpha": {
          "a": {
              "score": 1,
              "visit": 2
          },
          "b": {
              "score": 3,
              "visit": 4
          }
      },
      "beta" : {
          "a": {
              "score": 1,
              "visit": 2
          },
          "b": {
              "score": 3,
              "visit": 4
          }
      }
  }
  }
  
  dict2 = {
  "list": {
      "alpha": {
          "a": {
              "score": 1,
              "visit": 2
          },
          "c": {
              "score": 5,
              "visit": 6
          }
      },
      "beta" : {
          "a": {
              "score": 1,
              "visit": 2
          },
          "c": {
              "score": 5,
              "visit": 6
          }
      }
  }
  }
  </code></pre>
  
  <p>I want to merge dictionary like this</p>
  
  <pre><code>dict1 = {
  "list": {
      "alpha": {
          "a"  : {
              "score": 2,
              "visit": 4
          },
          "b": {
              "score": 3,
              "visit": 4
          },
          "c": {
              "score": 5,
              "visit": 6
          }
      },
      "beta": {
          "a": {
              "score": 2,
              "visit": 4
          },
          "b": {
              "score": 3,
              "visit": 4
          },
          "c": {
              "score": 5,
              "visit": 6
          }
      }
  }
  }
  </code></pre>
  
  <p>Condition 1. value is always new dictionary or int (not str)</p>
  
  <p>Condition 2. If the same key exists at the same depth, the value of that key must be sum.</p>
  
  <hr>
  
  <p>I think maybe I can solve this problem using for loops. 
  But Python seems to have a simpler and faster way.</p>
  
  <p>this is my best.</p>
  
  <p>code:</p>
  
  <pre><code>def logic(d1, d2, inconflict = lambda v1,v2 : v1+v2) :
      for k in d2:
          if k in d1 :
              if isinstance(d1[k], dict) and isinstance(d2[k], dict) :
                  logic(d1[k], d2[k], inconflict)
  
              elif isinstance(d1[k], int) :
                  d1[k] = inconflict(d1[k], d2[k])
          else :
              d1[k] = d2[k]
      return d1
  
  print logic(dict1, dict2)
  </code></pre>