fork download
  1. x = {
  2. 'a': {'z': 1},
  3. 'b': {'z': 2},
  4. 'c': {'z': 3},
  5. 'd': {'z': 4},
  6. 'e': {'z': 5},
  7. 'f': {'z': 6},
  8. 'g': {'z': 7}
  9. }
  10.  
  11. y = {
  12. 'b': {
  13. 'a': {},
  14. 'e': {
  15. 'c': {
  16. 'd': {}
  17. },
  18. 'f': {
  19. 'g': {}
  20. }
  21. }
  22. }
  23. }
  24.  
  25. def update(nested_dict, new_values):
  26. for key, value in nested_dict.items():
  27. if value:
  28. update(value, new_values)
  29. nested_dict[key].update(new_values[key])
  30.  
  31. update(y, x)
  32.  
  33. import json
  34. print(json.dumps(y, indent=2))
Success #stdin #stdout 0.04s 9676KB
stdin
Standard input is empty
stdout
{
  "b": {
    "e": {
      "z": 5,
      "f": {
        "z": 6,
        "g": {
          "z": 7
        }
      },
      "c": {
        "z": 3,
        "d": {
          "z": 4
        }
      }
    },
    "z": 2,
    "a": {
      "z": 1
    }
  }
}