Question
For the following dictionary:
{'id': 1, 'label': 'hello', 'remove_me': {'world': {'keep_me': 52}}}
I want to create a new dictionary without the remove_me or world keys.
Edit update:
To summarise, I wanted to do the following:
If the values of an item are a nested dictionary. Update the new dictionary with the inner result while removing the current key value from the main dict.
If the values of an item are not a nested dictionary, update the new dictionary with the key, value.
The accepted answer covers this.
What have I tried?
{k:v for (k,v) in d.items() if not k == 'remove_me'}
yields:
{'id': 1, 'label': 'hello'}
not exactly what I need as I'm dropping the nested dict.
Desired output:
{'id': 1, 'label': 'hello','keep_me': 52}