Consolidate list of dictionaries elements in dictionary

67 Views Asked by At

I have below complex structure with dictionaries with a number of repetitive keys. I want to consolidate the below structure and return the below structure. Tried multiple ways but not able to crack it. Please help.

Input structure:

{'lst_1': [{"127.0.0.1": [{“key1”:{“asm”: “entry1”, “CHG”: [“entry2”]}}]}, {"127.0.0.1": [{“key1”: {“PL”: “entry3”, “DU”: “entry4”}}]}, {"127.0.0.1": [{“key1”: {“SL”: “entry5”, “DU”: “entry6”}}]}], 'lst_0': [{"127.0.0.1": [{“key2”: {“asm”: “entry7”, “CHG”: [“entry8”]}}]}, {"127.0.0.1": [{“key2: {“PL”: “entry9”, “DU”: “entry10”}}]}, {"127.0.0.1": [{“key2”: {“SL”: “entry11”, “DU”: “entry12”}}]}]}

Output Structure:

{‘key1’: {‘asm’: ‘entry1’, 'SL': ‘entry5’, 'CHG’: [‘entry2’], 'DU': ‘entry6’, 'PL': ‘entry3’}, ‘key2’: {‘asm’: ‘entry7’, 'SL': ‘entry11’, 'CHG’: [‘entry8’], 'DU': ‘entry12’, 'PL': ‘entry9’}}
1

There are 1 best solutions below

1
On BEST ANSWER

This one does the job.

def filterDict(complex_dict):
    filtered_dict = {}

    for a in complex_dict.values():
        for b in a:
            for c in b.values():
                for d in c:
                    for e, f in d.items():
                        if e not in filtered_dict:
                            filtered_dict[e] = {}

                        filtered_dict[e].update(f)

    return filtered_dict

complex_dict = {'lst_0': [{'127.0.0.1': [{'key2': {'CHG': ['entry8'],
                                    'asm': 'entry7'}}]},
           {'127.0.0.1': [{'key2': {'DU': 'entry10',
                                    'PL': 'entry9'}}]},
           {'127.0.0.1': [{'key2': {'DU': 'entry12',
                                    'SL': 'entry11'}}]}],
 'lst_1': [{'127.0.0.1': [{'key1': {'CHG': ['entry2'],
                                    'asm': 'entry1'}}]},
           {'127.0.0.1': [{'key1': {'DU': 'entry4',
                                    'PL': 'entry3'}}]},
           {'127.0.0.1': [{'key1': {'DU': 'entry6',
                                    'SL': 'entry5'}}]}]}

print(filteredDict(complex_dict))

## OUTPUT:
# {'key1': {'CHG': ['entry2'],
#           'DU': 'entry6',
#           'PL': 'entry3',
#           'SL': 'entry5',
#           'asm': 'entry1'},
#  'key2': {'CHG': ['entry8'],
#           'DU': 'entry12',
#           'PL': 'entry9',
#           'SL': 'entry11',
#           'asm': 'entry7'}}

I guess there's nothing to explain here since this is just a bunch of nested loops to get the values needed for the filtered dictionary.

I'm quite curious why you have this kind of structure for your dictionary. If you can and able to, I suggest restructuring your dictionary, so you won't have to deal with this kind of nested loops.