I have a nested dictionary and I want to be able to delete an arbitrary key inside of it.
The dictionary could look like this:
D={'key1':{'key2':{'key3':'value3', 'key4':'value4'}, 'key5':'value5'}}
But it could be of arbitrary size. The problem is that the keys should be taken from a "key list" looking, for example, like this:
key_list = ['key1', 'key2', 'key4']
key_list
could be of arbitrary size and have any of the dictionary's keys in it.
Because of the above criteria, I can't just use:
del D['key1']['key2']['key4']
because I can't know beforehand which keys that key_list
will contain.
So, how would a generic code look like that based on the content of key_list
, deletes the corresponding item in the dictionary D
?