0
error:TypeError: list indices must be integers or slices, not dict

this is my code

for x in canidates:
if(canidates[x]['Department']==('che'or'CHE')):
    che.append(canidates[x]['reg number'])
elif(canidates[x]['Department']=='cl'or'CI'):
    ci.append(canidates[x]['reg number'])
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    ``for x in canidates:`` iterates over the *contents* of ``candidates``, not its indices. Use ``x`` instead of ``canidates[x]``. – MisterMiyagi Feb 23 '22 at 08:01
  • can you show what does `candidates` looks like? – Prats Feb 23 '22 at 08:01
  • First, indent your code and include the complete error message. Second, `canidates[x]['Department']==('che'or'CHE')` does not do what you think it does. – DYZ Feb 23 '22 at 08:01
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – DYZ Feb 23 '22 at 08:03
  • Hi @madhusankar, welcome to Stack Overflow! It would help us a bit if you could show what type of object `candidates` is. Your code might make sense if it was a dictionary, but the error message makes it seem likely that it's a list of dictionaries instead. It might also be a good idea to give the full traceback of the exception, since it's not entirely clear which line of your code is causing the error. Unrelated to the error, your use of `or` at the end of each of the conditions is not likely to work as you intend (it won't check the indexed value against both capitalizations). – Blckknght Feb 23 '22 at 09:26

2 Answers2

0

The for loop iterates over the elements in canidates, use this:

for x in canidates:
    if(x['Department']==('che'or'CHE')):
        che.append(x['reg number'])
    elif(x['Department']=='cl'or'CI'):
        ci.append(x['reg number'])

Also better to rename it:

for canidate in canidates:
    if(canidate ['Department']==('che'or'CHE')):
        che.append(canidate['reg number'])
    elif(canidate['Department']=='cl'or'CI'):
        ci.append(canidate['reg number'])

If you need the index, you can do this:

for index, canidate in enumerate(canidates):
    if(canidate ['Department']==('che'or'CHE')):
        che.append(canidate['reg number'])
    elif(canidate['Department']=='cl'or'CI'):
        ci.append(canidate['reg number'])
0

Please alter your code as follows

for x in range(len(canidates)) :
    if(canidates[x]['Department']==('che'or'CHE')) :
        che.append(canidates[x]['reg number'])
    elif(canidates[x]['Department']=='cl'or'CI') :
        ci.append(canidates[x]['reg number'])

You are trying to iterate over canidates, a list object, in your code and access each list item in the for-loop statement. Therefore, x is a list item, not a list index in the following statements in the body of the for-loop. Each list index must be an int or slice object.

To access x as list index, you need to know the length of canidates first. Same can be done using len(canidates). Then you can access each item in the canidates list object by their indices as follows :

for x in range(len(canidates)) :

Please mind the indentation levels in Python.

indiyaaah
  • 15
  • 2
  • 5