0

When a default template of dictionary is provided and an input source dictionary is present, how can a new dictionary with same nested structure as template be created while keeping the values for keys in input source as it is?

Example:

    template = {
                 keyA: A, 
                 keyB: B,
                 keyC: {keyC1: C1, 
                        keyC2: C2},
                 keyD: {keyD1: {keyD1A: D1A, 
                                keyD1B: D1B}, 
                        keyD2: D2}
               }


    source = {
               keyA: A-updated,
               keyD: {keyD1: {keyD1A: D1A-updated} 
                      keyD2: D2-updated}
             }


    output =  {
                 keyA: A-updated, 
                 keyB: B,
                 keyC: {keyC1: C1, 
                        keyC2: C2},
                 keyD: {keyD1: {keyD1A: D1A-updated, 
                                keyD1B: D1B}, 
                        keyD2: D2-updated}
               }
user3480922
  • 564
  • 1
  • 10
  • 22
  • 2
    Possible duplicate of [Update value of a nested dictionary of varying depth](https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth) – ComplicatedPhenomenon Aug 06 '19 at 07:51
  • Possible duplicate of [How to copy a dictionary and only edit the copy](https://stackoverflow.com/questions/2465921/how-to-copy-a-dictionary-and-only-edit-the-copy) –  Aug 06 '19 at 08:00
  • @ComplicatedPhenomenon Yes that solves the question. Thanks. – user3480922 Aug 06 '19 at 08:20

0 Answers0