jdigan jdigan -4 years ago 145
Python Question

I get the error, list indices must be integers, do not know why?

for i in inpt:
for j in inpt[i]:
print j,

I want to access a 2D array, for each array i in inpt, I want to print each number j in the array i. I do not have any formal background in python and also I could not already find a solution on the internet.

Answer Source

Python for loops are foreach loops, in that you get the actual elements from the list and not an index. You are trying to use those elements as indices to inpt again.

Loop over i, not inpt[i], to get the matrix values:

for i in inpt:
    for j in i:
        print j,

I'd rename i to row to make this clearer:

for row in inpt:
    for j in row:
        print j,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download