How to Recursively Remove None Values from Nested Dictionaries in Python
Recursively removes all None values from nested dictionaries and lists while preserving non-None data.
Python code
28 linesdef prune_none(obj):
if isinstance(obj, dict):
return {
k: prune_none(v)
for k, v in obj.items()
if v is not None and prune_none(v) is not None
}
elif isinstance(obj, list):
pruned = [prune_none(item) for item in obj]
pruned = [item for item in pruned if item is not None]
return pruned
else:
return obj
if __name__ == "__main__":
data = {
"name": "Alice",
"age": None,
"address": {
"street": "123 Main St",
"city": None,
"zip": None
},
"hobbies": ["reading", None, "hiking"],
"metadata": None
}
print(prune_none(data))
Output
{'name': 'Alice', 'address': {'street': '123 Main St'}, 'hobbies': ['reading', 'hiking']}
How it works
The function walks the data structure recursively: for dictionaries, it builds a new dict by filtering out keys whose value is None and also pruning values that become None after recursive processing for lists, it prunes None items and also recursively prunes nested structures.The base case is when the object is neither a dict nor a list, returning it unchanged. This approach preserves the original data structure, only removing None values without mutating the input.
Common mistakes
- Forgetting to prune None values inside lists, leaving `None` entries in nested lists.
- Modifying the original dict in place instead of creating a new pruned copy.
- Not handling the case where a nested dict becomes empty after pruning, leaving empty dicts.
Variations
- Use a stack or queue for iterative traversal to avoid recursion depth limits on deeply nested structures.
- Add a parameter to control whether empty containers ({} or []) should be removed after pruning.
Real-world use cases
- Cleaning API responses before storing them in a database, removing optional fields that were not provided.
- Sanitizing configuration dictionaries loaded from JSON files so that missing settings are not passed to functions.
- Preparing data for serialization by removing None values that would otherwise cause validation errors in downstream systems.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.