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.

Medium Python 3.9+ Aug 9, 2026 Dictionaries & sets 15 views 0 copies

Python code

28 lines
Python 3.9+
def 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

stdout
{'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

  1. Use a stack or queue for iterative traversal to avoid recursion depth limits on deeply nested structures.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.