3
3
3ds2012-03-23 10:18:27
Python
3ds, 2012-03-23 10:18:27

Python dictionary change value along path?

Hello, tell me how to change the value in the dictionary along the way in the most beautiful way. A dictionary can contain both dictionaries and lists inside.
For example dictionary:

{ &quot;test1&quot; : &quot;hello&quot;,<br/>
 &quot;test2&quot; : [ &quot;ok&quot;, &quot;ok1&quot;, &quot;ok2&quot; ],<br/>
 &quot;test3&quot; : { &quot;sub1&quot; : [0,1,2,3],<br/>
 &quot;sub2&quot; : 1}<br/>
}

And the path + value:
test3/sub1/2 + 4
i.e. after the change, the dictionary should become like this:
{ &quot;test1&quot; : &quot;hello&quot;,<br/>
 &quot;test2&quot; : [ &quot;ok&quot;, &quot;ok1&quot;, &quot;ok2&quot; ],<br/>
 &quot;test3&quot; : { &quot;sub1&quot; : [0,1,4,3],<br/>
 &quot;sub2&quot; : 1}<br/>
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
3
3ds, 2012-03-23
@3ds

Here is the solution.
gist.github.com/2168749

B
bagyr, 2012-03-23
@bagyr

dict['test3']['sub1'][2] = 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question