I am using Python 2.7 with Windows 7.
I have a dictionary and would like to remove values that correspond to (key, value) pairs from another dictionary.
For example, I have a dictionary t_dict
. I would like to remove corresponding (key,value) pairs that are in the dictionary values_to_remove
so that I end up with dictionary final_dict
t_dict = {
'a': ['zoo', 'foo', 'bar'],
'c': ['zoo', 'foo', 'yum'],
'b': ['tee', 'dol', 'bar']
}
values_to_remove = {
'a': ['zoo'],
'b': ['dol', 'bar']
}
# remove values here
print final_dict
{
'a': ['foo', 'bar'],
'c': ['zoo', 'foo', 'yum'],
'b': ['tee']
}
I have looked at similar pages on SO and the python dictionaries doc but cannot find anything to solve this specific problem:
https://docs.python.org/2/library/stdtypes.html#dict
How to remove dictionaries with duplicate values from a nested dictionary
How to remove a key from a python dictionary?
EDIT
There cannot be duplicate values in t_dict
per key. For example there will never be
t_dict['a'] = ['zoo','zoo','foo','bar']