access nested set with array params python

Solutions on MaxInterview for access nested set with array params python by the best coders in the world

showing results for - "access nested set with array params python"
Aarón
13 Sep 2016
1def nested_set(dic, keys, value):
2    for key in keys[:-1]:
3        dic = dic.setdefault(key, {})
4    dic[keys[-1]] = value
5    
6>>> d = {}
7>>> nested_set(d, ['person', 'address', 'city'], 'New York')
8>>> d
9{'person': {'address': {'city': 'New York'}}}
10
11from : 
12https://stackoverflow.com/questions/13687924/setting-a-value-in-a-nested-python-dictionary-given-a-list-of-indices-and-value