title: List Indices Must Be Integers or Slices, not Dict
body: <p>I am doing some operations that involves extracting data from a list where every element is a dictionary. Each dictionary contains two key-value pairs which are a string and then an int (i.e {'ID':0, 'Zip Code':9414}), and then a key-value pair where the key is a string, and then a list ({'Value':[0,0,1,1,0,1]})</p>
  
  <p>I'm able to access the values inside that list in a dictionary in a list very easily. However, since there are a bunch of elements in the list, I have to use a for loop to go through it. Basically what my method does is check if a 1 is at index(user specified number) in that list in a dict in a list. If it is, it updates another list with the first two key-value pairs from the same dict.</p>
  
  <p>So, like this:</p>
  
  <pre><code>import returnExternalList #this method returns a list generated by an external method
  
  def checkIndex(b):
      listFiltered = {}
      listRaw = returnExternalList.returnList #runs the method "returnList", which will return the list
      for i in listRaw:
          if listRaw[i]['Value'][b] == 1:
              filteredList.update({listRaw[i]['ID']: listRaw[i]['Zip Code']})
      print(filteredList)
  
  checkIndex(1)
  
  returnExternalList.returnList:
  [{'ID':1 ,'Zip Code':1 ,'Value':[0,1,0,0,1]},{'ID':2 ,'Zip Code':2 ,'Value':[0,0,0,0,0]},{'ID':3,'Zip Code':3 ,'Value':[0,1,1,1,0]},{'ID':4 ,'Zip Code':4 ,'Value':[1,0,0,0,0]}]
  
  expected output:
  [{1:1 , 3:3}]
  </code></pre>
  
  <p>I can access values in a list inside a dictionary inside a list <em>outside</em> of a for loop very simply just by doing this:</p>
  
  <pre><code>print(listRaw[0]['Value'][1]) would return 1, for example.
  </code></pre>
  
  <p>However, when attempting to replicate that behaviour with the for loop to check every single one in the list, I get the error:</p>
  
  <p><code>TypeError: list indices must be integers or slices, not dict</code></p>
  
  <p>What do I do?</p>
  
  <p>EDIT: Since it was asked for, returnExternalList:</p>
  
  <pre><code>def returnList:
      listExample = [{'ID':1 ,'Zip Code':1 ,'Value':[0,1,0,0,1]},{'ID':2 ,'Zip Code':2 ,'Value':[0,0,0,0,0]},{'ID':3,'Zip Code':3 ,'Value':[0,1,1,1,0]},{'ID':4 ,'Zip Code':4 ,'Value':[1,0,0,0,0]}]
       return listExample  
  </code></pre>
  
  <p>EDIT: I used two of the solutions offered below, and while it does get rid of the error (thank you!) the output is simply a blank dictionary.</p>
  
  <p>Code:</p>
  
  <pre><code>for i in listRaw:
      if i['Value'][b] == 1:
         filteredList.update({i['ID']: i['Zip Code']})
  
  or
  
  for i in range(len(listRaw):
      if listRaw[i]['Value'][b] == 1:
          filteredList.update({listRaw[i]['ID']: listRaw[i]['Zip Code']}) 
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>It works now, the reason the list was empty was because I was comparing 1 to '1'. It's been fixed. Thank you.</p>