Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to recursively iterate a nested Python dictionary?
A Dictionary in Python is a mutable collection of data in a key-value format. A dictionary can also contain another dictionary as a value, creating a Nested Dictionary or a dictionary within a dictionary. When working with nested data structures, you often need to access each key-value pair at every level using recursive iteration.
In this article, we will explore how to recursively iterate through a nested Python dictionary using different approaches.
Basic Syntax
Here's the basic syntax for recursively iterating through a nested dictionary ?
def recursive_iter(d):
for key, value in d.items():
if isinstance(value, dict):
recursive_iter(value)
else:
print(key, ":", value)
Simple Recursive Iteration
This example shows basic recursive iteration that prints all key-value pairs ?
def recursive_iter(d):
for key, value in d.items():
if isinstance(value, dict):
print(f"Entering nested dictionary at key: {key}")
recursive_iter(value)
else:
print(f"{key}: {value}")
# Nested Dictionary
data = {
'person': {
'name': 'John',
'age': 30,
'address': {
'city': 'New York',
'zip': '10001'
}
},
'job': {
'title': 'Developer',
'department': 'Engineering'
}
}
# Call the function
recursive_iter(data)
Entering nested dictionary at key: person name: John age: 30 Entering nested dictionary at key: address city: New York zip: 10001 Entering nested dictionary at key: job title: Developer department: Engineering
Collecting All Values in a List
This approach collects all values from the nested dictionary into a single list ?
def collect_values(d, result=None):
if result is None:
result = []
for key, value in d.items():
if isinstance(value, dict):
collect_values(value, result)
else:
result.append(value)
return result
# Example usage
data = {
'user': {
'profile': {
'name': 'Alice',
'email': 'alice@email.com'
},
'preferences': {
'theme': 'dark',
'language': 'en'
}
},
'status': 'active'
}
values = collect_values(data)
print("All values:", values)
All values: ['Alice', 'alice@email.com', 'dark', 'en', 'active']
Finding Specific Keys
This method recursively searches for a specific key and returns its value ?
def find_key(d, target_key):
for key, value in d.items():
if key == target_key:
return value
elif isinstance(value, dict):
result = find_key(value, target_key)
if result is not None:
return result
return None
# Example usage
data = {
'config': {
'database': {
'host': 'localhost',
'port': 5432
},
'cache': {
'host': 'redis-server',
'port': 6379
}
}
}
port = find_key(data, 'port')
print(f"First port found: {port}")
host = find_key(data, 'host')
print(f"First host found: {host}")
First port found: 5432 First host found: localhost
Building Full Key Paths
This approach tracks the full path to each value using dot notation ?
def get_paths(d, parent_key=''):
paths = []
for key, value in d.items():
current_key = f"{parent_key}.{key}" if parent_key else key
if isinstance(value, dict):
paths.extend(get_paths(value, current_key))
else:
paths.append((current_key, value))
return paths
# Example usage
data = {
'server': {
'web': {
'port': 80,
'ssl': True
},
'database': {
'port': 3306,
'name': 'mydb'
}
}
}
paths = get_paths(data)
for path, value in paths:
print(f"{path}: {value}")
server.web.port: 80 server.web.ssl: True server.database.port: 3306 server.database.name: mydb
Conclusion
Recursive iteration is essential for processing nested dictionaries in Python. Use simple recursion for basic traversal, collect values for data extraction, or build key paths for complex nested structures. Choose the approach that best fits your specific use case.
