How to get variable by reference?

In the options comes to the path variable relative to the entry point:
How to get the object and change it from the link? not to invent bicycles.

entry = self.companies

for path in options['path']: # options['path'] == ['up', 'middle', 'down']
 entry = entry[path] # Need to get the object: self.companies['up']['middle']['down'] and change it

entry = options['value']
June 10th 19 at 16:02
2 answers
June 10th 19 at 16:04
Solution
you have a nested object and its path, it is necessary to replace the last a nested object value, you can do so
d = {1:{2:{3:{4:{5:0}}}}}
b = [1,2,3,4,5]
c = reduce(lambda d1, x: d1.get(x), b[:-1], d)
c
c[b[-1]] = 5
d
{1: {2: {3: {4: {5: 5}}}}}
June 10th 19 at 16:06
Solution
Develoeper this:

entry = self.companies

entries = {}
for index, path in enumerate(options['path']): # options['path'] == ['up', 'middle', 'down']
 if index == 0:
 entries[index] = entry[path]
 else: 
 entries[index] = entries[index - 1][path]

path_length = len(options['path'])
entries[path_length - 2][options['path'][path_length - 1]] = options['value']


Are there any other options?

Find more questions by tags Python