0

I have a 2d list of board positions where some are true some are false.

I want to iterate through them all while still having the indexes inside the nest.

aka:

for x,y in 2darray:
   if 2darray[x][y] == true: print x,y
1
  • 1
    What exactly is 2darray? A list of lists (that is not an array) or some sort of numpy array? Commented Jan 28, 2017 at 3:50

2 Answers 2

2

lets assume the name of 2darray is x

for i in range(len(x)):
    for j in range(len(x[i])):
        if x[i][j] == true :
             print (i,j)
Sign up to request clarification or add additional context in comments.

Comments

1

Or you can use list comprehensions:

if l is your 2darray, then you can do the following

for i, j in [(i, j) for i in xrange(len(l)) for j in xrange(len(l[i]))]:
    if l[i][j]:
        print (i, j)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.