Find All Leaf Paths in a Nested Dict in Python
Recursively traverse a nested dictionary and yield every leaf path as a list of keys, including paths to empty dictionaries.
Python code
21 linesdef find_leaf_paths(data, path=None):
if path is None:
path = []
if not isinstance(data, dict) or not data:
yield path
return
for key, value in data.items():
yield from find_leaf_paths(value, path + [key])
if __name__ == "__main__":
nested = {
"a": 1,
"b": {"c": 2, "d": {"e": 3}},
"f": {},
"g": {"h": {"i": 4}}
}
for leaf_path in find_leaf_paths(nested):
print(leaf_path)
Output
['a']
['b', 'c']
['b', 'd', 'e']
['f']
['g', 'h', 'i']
How it works
The function treats both non-dict values and empty dicts as leaves, so every branch terminates with a path. The path is None check avoids the mutable default argument pitfall, creating a fresh list for each top-level call. yield from recursively delegates to sub-traversals, combining paths with the + operator to build the key chain. This fits dictionary traversal patterns where you need to locate all terminal nodes in arbitrarily nested data.
Common mistakes
- Using a mutable default argument like `path=[]`, which causes shared state across calls
- Forgetting to treat empty dicts as leaf nodes, making the recursion miss terminal branches
- Checking `isinstance(data, dict)` after trying to iterate, risking a TypeError on non-dict values
Variations
- Use a stack for an iterative depth-first traversal instead of recursion
- Collect all paths into a list with `list(find_leaf_paths(data))`
Real-world use cases
- Flattening JSON API responses to map every endpoint field to a location for data migration.
- Walking configuration dictionaries to validate every setting key and surface missing or empty leaf values.
- Instrumenting policy or rules trees where each leaf path identifies a specific decision endpoint.
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.