Table of Contents
Motivation
Sometimes a task which should be easy can be hard to accomplish in practice.
Manipulating nested dictionaries in Python seems to be such a task.
Without dotty
Accessing nested dictionaries looks like this:
data = {
'a': {
'b': {
'c': 'd'
}
}
}
assert data['a']['b']['c'] == 'd'
You have to chain the keys to get down to the nested dict and get the value for that key
With dotty
Installation
You can install dotty with
pip install dotty-dict
Usage
from dotty_dict import dotty
data = {
'a': {
'b': {
'c': 'd'
}
}
}
dot = dotty(data)
assert 'a.b' in dot
assert dot['a.b.c'] == 'd'
After importing dotty you can wrap your existing vanilla Python dictionary in a dotty object.
Now you can access your nested values by chaining the key with the dot to a “key path”
Updating dictionaries
data = {
'a': {
'b': {
'c': 'd'
}
}
}
dot = dotty(data)
dot['a.b.c'] = 'e'
assert dot['a.b.c'] == {'c': 'e'}
Finding the key
If you already know the “path” to the nested dictionary you will be fine.
But if you just know the most inner key you can use this little recursive function to extract the full path to the nested dictionary:
def _get_key_path(data, key, path=None):
found = False
if path is None:
path = []
for k, v in data.items():
if k == key:
path.append(key)
found = True
break
elif isinstance(v, dict):
path.append(k)
_path, found = _get_key_path(v, key, path)
if not found:
path.pop()
return path, found
def get_key_path(data, key):
key_path, _ = _get_key_path(data, key)
key_path = ".".join(key_path)
return key_path