I\'m trying to programmatically set a value in a dictionary, potentially nested, given a list of indices and a value.
So for example, let\'s say my list of indices i
Something like this could help:
def nested_set(dic, keys, value): for key in keys[:-1]: dic = dic.setdefault(key, {}) dic[keys[-1]] = value
And you can use it like this:
>>> d = {} >>> nested_set(d, ['person', 'address', 'city'], 'New York') >>> d {'person': {'address': {'city': 'New York'}}}