0

I have two dictionaries as:

    dict1 = {'Warning': [{'User account': 'User doesnot exist'},
                         {'Login': 'Login failed'}],
             'Error': [{'Monitoring': 'Monitoring failed due to system crash'},
                       {'Scheduler': 'Scheduler stopped running'}]}

dict2 = {'Warning': [{'User account': 'User doesnot exist'},
                     {'Login': 'Multiple logins attempted'},
                     {'Version': 'Version doesnot match'}],
         'Error': [{'Monitoring': 'Monitoring failed due to system crash'},
                   {'Scheduler': 'Scheduler never inititated'}],
         'Critical': [{'Memory': 'Memory overflow'}]}

I have to compare two dictionaries and if the values(which is list of dictionaries) of dict2 are not common in dict1, have to return those: The expected output is:

result = {'Warning': [{'Login': 'Multiple logins attempted'},
                      {'Version': 'Version doesnot match'}],
          'Error': [{'Scheduler': 'Scheduler never inititated'}],
          'Critical': [{'Memory': 'Memory overflow'}]}

Any help is highly appreciated.

gd1
  • 655
  • 1
  • 10
  • 21

1 Answers1

0

You can try the following approach:

  • Iterate over the first dict2 key/value level:

    • For each key, check if the key exist in dict1.
    • If it doesn't exist: add all the values to the output
    • Else:
      • Transform the list of dict in one dict based on the discussion
      • Iterate over key/values of the new dict (from dict2):
      • If the key doesn't exist in d1 dict or the values are different:
        • Add the {key/value} to the list of dict
      • Add the list of dict to the output

Full code:

dict1 = {'Warning': [{'User account': 'User doesnot exist'},
                     {'Login': 'Login failed'}],
         'Error': [{'Monitoring': 'Monitoring failed due to system crash'},
                   {'Scheduler': 'Scheduler stopped running'}]}

dict2 = {'Warning': [{'User account': 'User doesnot exist'},
                     {'Login': 'Multiple logins attempted'},
                     {'Version': 'Version doesnot match'}],
         'Error': [{'Monitoring': 'Monitoring failed due to system crash'},
                   {'Scheduler': 'Scheduler never inititated'}],
         'Critical': [{'Memory': 'Memory overflow'}]}


out = {}
for k, l in dict2.items():
    # Check first key exist: if not -> add all values
    if k not in dict1:
        out[k] = l
    else:
        # New list of dict
        new_v = []
        # Transform the list of dict into one dictionnary
        sub_d1 = {k: v for d in dict1[k] for k, v in d.items()}
        sub_d2 = {k: v for d in l for k, v in d.items()}
        # Iterate over dict 2 values
        for k, v in sub_d2.items():
            # If d2 keys not in d1 key or values are differents:
            if k not in sub_d1.keys() or sub_d1[k] != v:
                # add key:value to list of dict
                new_v.append({k,v})
        # Append new list of dict if not empty
        if len(new_v) > 0:
            out[v] = new_v

print(out)
# {
#     'Version doesnot match': [{'Login', 'Multiple logins attempted'},
#                               {'Version doesnot match', 'Version'}],
#     'Scheduler never inititated': [{'Scheduler', 'Scheduler never inititated'}],
#     'Critical': [{'Memory': 'Memory overflow'}]
# }
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40