If A and B are two dictionaries, using python, is there any way of removing elements from Dictionary A that are in dictionary B?
For example,
parent_dict = {"a" : "aaa", "b" : "bbb", "c" : "ccc", "d" : "ddd", "e": "eee"}
derived_dict = {"a" : "aaa", "d" : "ddd", "e" : "eee"}
Now I need to write a function dict_reduce(dictA, dictB) which deletes all the elements of dictB from dictA.
(i.e.,) dict_reduce(parent_dict, derived_dict) should give {"b" : "bbb", "c" : "ccc"}
My work around with a for loop is:
def dict_reduce(parent_dict, child_dict):
    for key in child_dict.keys():
        del parent_dict[key]
    return parent_dict
reduced_dict = dict_reduce(parent_dict, child_dict)
NOTE:
{k: v for k, v in parent_dict.items() if k not in derived_dict}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With