set_nested(d, keys, value) where keys is a list of keys describing a path (e.g. ["a", "b", "c"]), and value is the value to place at that path. Create any missing intermediate dicts along the way, then set the final key to value. Mutate d in place and return it.d = {}; set_nested(d, ['a', 'b', 'c'], 42); d{'a': {'b': {'c': 42}}}Every missing level is created as an empty dict, then the leaf is set.
d = {'a': {'x': 1}}; set_nested(d, ['a', 'b'], 2); d{'a': {'x': 1, 'b': 2}}Existing sibling keys are preserved.
Walk keys[:-1], using `current = current.setdefault(k, {})` at each step.
Then set `current[keys[-1]] = value`.