Delete a dictionary from another dictionary python -



Delete a dictionary from another dictionary python -

if , b 2 dictionaries, using python, there way of removing elements dictionary 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 need write function dict_reduce(dicta, dictb) deletes elements of dictb dicta.

(i.e.,) dict_reduce(parent_dict, derived_dict) should give {"b" : "bbb", "c" : "ccc"}

my work around loop is:

def dict_reduce(parent_dict, child_dict): key in child_dict.keys(): del parent_dict[key] homecoming parent_dict reduced_dict = dict_reduce(parent_dict, child_dict)

note:

it great if solution 1 liner or not takes loop. also need not check whether parent dictionary has key before deleting, since kid dictionary derived parent dictionary. need not think keyerror. the parent dictionary static dictionary should not affected method. instead returning dictionary should stored in reduced dictionary. there no need check whether child_dict has same key of parent_dict. key matters.

{k: v k, v in parent_dict.items() if k not in derived_dict}

python dictionary

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -