How do I filter a nested dictionary in python based on key values:
d = {'data': {'country': 'US', 'city': 'New York', 'state': None},
'tags': ['US', 'New York'],
'type': 'country_info',
'growth_rate': None
}
I want to filter this dictionary to eliminate NoneType values so the resulting dict should be:
d = {'data': {'country': 'US', 'city': 'New York'},
'tags': ['US', 'New York'],
'type': 'country_info',
}
Also, the dict can have multiple levels of nesting. I want to remove all NoneType values from the dict.
You can define this recursively pretty easily with a dict comprehension.
Recursion isn’t too optimised in Python, but given the relatively small number of nestings that are likely, I wouldn’t worry.
Looking before we leap isn’t too Pythonic, I think it is a better option than catching the exception – as it’s likely that the value will not be a
dictmost of the time (it is likely we have more leaves than branches).Also note that in Python 2.x, you probably want to swap in
iteritems()foritems().