TypeError: list indices must be integers or slices, not list

Michael picture Michael · May 19, 2016 · Viewed 56k times · Source

array = some kind of list with 3 columns and unlimited amount of rows with data inside of it.

Volume = array[0][2] 
counter = 0
for i in array: 
    if Volume == array[i][2]: #<------ why is this line a problem? 
        counter += 1

Answer

Piotr Ćwiek picture Piotr Ćwiek · May 19, 2016

This is a classic mistake. i in your case is already an element from array (i.e. another list), not an index of array (not an int), so

if Volume == i[2]:
    counter += 1

You can check the Python tutorial. Also, try doing this:

for i in array:
    print (i)

And see what you get!

Also I would advise to stick to naming conventions: variables are normally lower-case (volume, not Volume). In this case i is misleading. row or elem would be much more suitable.