Replace keys in a nested dictionary

Question:

I have a nested dictionary {1: {2: {3: None}}} and a dictionary that maps keys of the nested dictionary to a set of values such as {1: x, 2: y, 3: z}. I want to transform the nested dictionary to this form {x: {y: {z: None}}}. I have tried a couple of recursive functions but I keep going in circles and confusing myself. What is the best way to achieve this?

The level of nesting is arbitrary. The above is a simple example.

Asked By: Bilentor

||

Answers:

You need to recurse through the dictionary while building a new one with new keys. Note that if you have a list or tuple in there somewhere that has other dictionaries in it, they won’t be processed – you’d have to add some code to do that. You can actually do this without building a new dictionary, but I think this way is simpler.

od = { 1: { 2: { 3: None }}}
kd = { 1: 'x', 2: 'y', 3: 'z' }

def replace_keys(old_dict, key_dict):
    new_dict = { }
    for key in old_dict.keys():
        new_key = key_dict.get(key, key)
        if isinstance(old_dict[key], dict):
            new_dict[new_key] = replace_keys(old_dict[key], key_dict)
        else:
            new_dict[new_key] = old_dict[key]
    return new_dict

nd = replace_keys(od, kd)
print nd

outputs:

{'x': {'y': {'z': None}}}
Answered By: Fhaab

The accepted answer will not support dict of list, adding the full feature

@bilentor,

od = {'name': 'John', '1': [{'name': 'innername'}]}
kd = { 'name': 'cname', '1': '2', 3: 'z' }

def replace_keys(data_dict, key_dict):
    new_dict = { }
    if isinstance(data_dict, list):
        dict_value_list = list()
        for inner_dict in data_dict:
            dict_value_list.append(replace_keys(inner_dict, key_dict))
        return dict_value_list
    else:
        for key in data_dict.keys():
            value = data_dict[key]
            new_key = key_dict.get(key, key)
            if isinstance(value, dict) or isinstance(value, list):
                new_dict[new_key] = replace_keys(value, key_dict)
            else:
                new_dict[new_key] = value
        return new_dict

nd = replace_keys(od, kd)
print(nd)
Answered By: Harry

You can use a NestedDict

from ndicts import NestedDict

d = {1: {2: {3: None}}}
replace = {1: 'x', 2: 'y', 3: 'z'}

def ndict_replace(ndict: dict, map: dict):
    nd = NestedDict(nd)
    new_nd = NestedDict()
    for key, value in nd.items():
        new_key = tuple(replace.get(k, k) for k in key)
        new_nd[new_key] = value
    return new_nd.to_dict()
>>> ndict_replace(d, replace)
{'x': {'y': {'z': None}}}

The solution is robust and works with any nested dictionary

>>> d = {
        1: {2: {3: None}}, 
        3: {4: None},
        5: None
    }
>>> ndict_replace(d, replace)
{'x': {'y': {'z': None}}, 'z': {4: None}, 4: None}}

To install ndicts pip install ndicts

Answered By: edd313