I have a nested dictionary, and I want to be able to remove an arbitrary key from it.
A dictionary might look like this:
D={'key1':{'key2':{'key3':'value3', 'key4':'value4'}, 'key5':'value5'}}
But it can be of any size. The problem is that the keys must be taken from the "key list", for example, for example:
key_list = ['key1', 'key2', 'key4']
key_list can be of arbitrary size and contain any dictionary keys in it.
Due to the above criteria, I cannot just use:
del D['key1']['key2']['key4']
because I cannot know in advance which keys the key_list will contain.
So, what will the general code look like based on the contents of key_list , delete the corresponding element in the D dictionary?
source share