I have a dictionary of deeply nested dictionaries and I am trying to remove all k-v pairs that are None or "". The below dictionary d is an example of the input.
d = {
"1": {
"1_1": 'a real value',
"1_2": ""
},
"2": None,
"3": {
"3_1": {
"3_2": None
}
}
}
Normally, to remove empty fields in a dictionary, the command {k:v for k,v in d.items() if v} does the job. But in this case, I want to remove all the nested dictionaries whose values are empty or null, and I can't figure out how to do this. Any ideas?
After d passes through this transformation, all the empty dictionaries whose values are empty should be gone. It should look like this:
{
"1": {
"1_1": 'a real value',
}
}