Home > Software design >  How to combine nested dictionaries in for loop
How to combine nested dictionaries in for loop

Time:09-17

I have a question regarding nested dictionaries in python.

I have two dictionaries, dict1 and dict2. dict2 contains some definitions (nested_dict_a/_b/_c/_d) which are used with repetition in the last level of dict1 (which is why I have them in separate dictionaries).

dict1 = {
    'nested_dict_1':  {
         'x': ['nested_dict_a', 'nested_dict_b'],
         'y': ['nested_dict_a', 'nested_dict_c'],
         'z': ['nested_dict_b', 'nested_dict_d']
    }
}

dict2 = {
    'nested_dict_1':  {
          'nested_dict_a': {
              1: {
                  'abc': 'bcd',
                  'cde': 'def'
                 },
              0: {
                  'efg': 'fgh',
                  'ghi': 'hij'
                 },
          },
          'nested_dict_b': {
              1: {
                  'ijk': 'jkl',
                  'klm': 'lmn'
                 },
              0: {
                  'mno': 'nop',
                  'opq': 'pqr'
                 },
          },
          'nested_dict_c': {
              1: {
                  'qrs': 'rst',
                  'stu': 'tuv'
                 },
              0: {
                  'uvw': 'vwx',
                  'wxy': 'xyz'
                 },
          },
          'nested_dict_d': {
              1: {
                  'abcd': 'bcde',
                  'cdef': 'defg'
                 },
              0: {
                  'efgh': 'fghi',
                  'ghij': 'hij'
                 },
          }
    }
}

I would like to combine the information of both nested dictionaries into a single dictionary, dict3. dict3 would be similar to dict1, only replacing the values in the lists (i.e. in 'x', 'y', 'z') with the corresponding dictionaries from dict2 (i.e. nested_dict_a/_b/_c/_d).

'x' in dict3 would then look like this:

     'x': {
          'nested_dict_a': {
              1: {
                  'abc': 'bcd',
                  'cde': 'def'
                 },
              0: {
                  'efg': 'fgh',
                  'ghi': 'hij'
                 },
          },
          'nested_dict_b': {
              1: {
                  'ijk': 'jkl',
                  'klm': 'lmn'
                 },
              0: {
                  'mno': 'nop',
                  'opq': 'pqr'
                 },
          }
     }

The procedure would be similar for 'y' and 'z'.

I believe that I have set up the correct for loop:

for a in dict1:
    for b in dict1[a]:
        for c in dict1[a][b]:
            for d in dict2[a][c]:

However, I do not know how to create the nested dictionary out of the local variables in the four loops.

Something like this does not work:

{a: {dict1[a]: {dict1[a][b]: {dict2[a][c]: d}}}}

CodePudding user response:

Iterate dict1 using for loops and use reference of dict2 to create a new dict.

dict3 = {}
# k1: nested_dict_1, v1: Object
for k1, v1 in dict1.items():
  dict3[k1] = {}
  for k11, v11 in v1.items():
    # k11: x, v11: array
    dict3[k1][k11] = {}
    for v111 in v11:
      # v111: nested_dict_a
      # Using k1 and v111 access the object from dict2 and use it in th new dict
      dict3[k1][k11][v111] = dict2[k1][v111]

print(dict3)
  • Related