jdigan
jdigan

Reputation: 55

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.

Upvotes: 0

Views: 46

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122132

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,

Upvotes: 1

Related Questions