title: How to use the index in an enumerate for loop with if statement (python)
body: <p>I have a nested dictionary, of which it is possible to access a value like this:</p>
  <p><code>    print(list['sublist'][0]['item'][0]['commentId'])</code></p>
  <p>However if I create a for loop and would like to use the index like this it gives me a keyerror:</p>
  <pre><code>    for index,sublist in enumerate(list['sublist']):
          if sublist[index]['item'][0]['commentId'] == commentid:
              number = sublist[index]['item'][0]['positionId']
              username = sublist[index]['subListCreator']
      print(number)
      print(username)
  </code></pre>
  <p>The error:</p>
  <pre><code>, in send_async_notification
      if sublist[index]['item'][0]['commentId'] == commentid:
    File &quot;/Users/ ...... /venv/lib/python3.8/site-packages/mongoengine/base/document.py&quot;, line 263, in __getitem__
      raise KeyError(name)
  </code></pre>
  <p>The whole goal of this exercise is to get two particular values in a nested array based on a deeper nested commentId. If there is a match, I would like to know the positionId and the subListCreator underneath it. An item can only have one commentId, hence the [0] in the code.</p>
  <p>It would also be ok to just have the whole dictionary &quot;branch&quot; that matches the commentId, this would also make things work.</p>
  <p>Btw, <code>print(commentid)</code> gives me the commentid I would like to match against. Just to explain its present.</p>
  <p>Hope there's a solution here, wonder why it's not working. Looking forward to any answer!</p>