title: Python: Merging two arbitrary data structures
body: <p>I am looking to efficiently merge two (fairly arbitrary) data structures: one representing a set of defaults values and one representing overrides. Example data below. (Naively iterating over the structures works, but is very slow.) Thoughts on the best approach for handling this case?</p>
  
  <pre class="lang-python prettyprint-override">
  
  _DEFAULT   = { 'A': 1122, 'B': 1133, 'C': [ 9988, {            'E': [ { 'F': 6666,            },       ], }, ], }
  
  _OVERRIDE1 = {            'B': 1234, 'C': [ 9876, { 'D': 2345, 'E': [ { 'F': 6789, 'G': 9876, }, 1357, ], }, ], }
  _ANSWER1   = { 'A': 1122, 'B': 1234, 'C': [ 9876, { 'D': 2345, 'E': [ { 'F': 6789, 'G': 9876, }, 1357, ], }, ], }
  
  _OVERRIDE2 = {                       'C': [ 6543, {            'E': [ {            'G': 9876, },       ], }, ], }
  _ANSWER2   = { 'A': 1122, 'B': 1133, 'C': [ 6543, {            'E': [ { 'F': 6666, 'G': 9876, },       ], }, ], }
  
  _OVERRIDE3 = {            'B': 3456, 'C': [ 1357, { 'D': 4567, 'E': [ { 'F': 6677, 'G': 9876, }, 2468, ], }, ], }
  _ANSWER3   = { 'A': 1122, 'B': 3456, 'C': [ 1357, { 'D': 4567, 'E': [ { 'F': 6677, 'G': 9876, }, 2468, ], }, ], }
  
  </pre>
  
  <p>This is an example of how to run the tests:
  (The dictionary update doesn't work, just an stub function.)</p>
  
  <pre class="lang-python prettyprint-override">
  
      import itertools
  
      def mergeStuff( default, override ):
          # This doesn't work
          result = dict( default )
          result.update( override )
          return result
  
      def main():
          for override, answer in itertools.izip( _OVERRIDES, _ANSWERS ):
             result = mergeStuff(_DEFAULT, override)
             print('ANSWER:   %s'   % (answer) )
             print('RESULT:   %s\n' % (result) )
  
  </pre>