When you use the syntax for variable in iterable:
, the variable
is taking the values of the iterable, not their indexes as you suppose in your code.
That is, in the first iteration of your loop, the i
will take the value of the first element that has the list. And that element is another list ( [1,2,3]
). In the second iteration i
will contain the next element, which is another list.
Bearing in mind that in each iteration (and in your case) the variable contains a row of your array, it would be better to call fila
to that variable, instead of i
. In this way the code is more readable:
for fila in array:
print(fila[1])
Another option you have is to iterate through the indexes instead of the values. This is what is usually done in other languages such as C. The code is more similar to what you tried, but it is less pythonic . To do so, you need a list of indexes and typically use the range()
function to build one. So:
for i in range(len(array)):
print(array[i][1])
Note . The error that python gave you makes all the sense, since in your code, in the expression array[i][1]
, the i
that you were putting inside the brackets was another list and not an integer. That's why I tell you " list indices must be integer or slices, not list ".