title: Adding a new key to a nested dictionary in python
body: <p>I need to add a key with a value that increases by one for every item in the nested dictionary. I have been trying to use the <code>dict['key']='value'</code> syntax but can't get it to work for a nested dictionary. I'm sure it's a very simple. </p>
  
  <p>My Dictionary:</p>
  
  <pre><code>mydict={'a':{'result':[{'key1':'value1','key2':'value2'},
                          {'key1':'value3','key2':'value4'}]}}
  </code></pre>
  
  <p>This is the code that will add the key to the main part of the dictionary:</p>
  
  <pre><code>for x in range(len(mydict)):
          number = 1+x
          str(number)
          mydict[d'index']=number
  
  print mydict
    #out: {d'index':d'1',d'a'{d'result':[...]}}
  </code></pre>
  
  <p>I want to add the new key and value to the small dictionaries inside the square parentheses:</p>
  
  <pre><code>{'a':{'result':[{'key1':'value1',...,'index':'number'}]}}
  </code></pre>
  
  <p>If I try adding more layers to the last line of the <code>for loop</code> I get a traceback error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Python27\program.py", line 34, in &lt;module&gt;
      main()
    File "C:\Python27\program.py", line 23, in main
      mydict['a']['result']['index']=number
  TypeError: list indices must be integers, not unicode
  </code></pre>
  
  <p>I've tried various different ways of listing the nested items but no joy. Can anyone help me out here?</p>