Reputation: 1
I am having a 2D array.
grid[0][0]= hat
grid[0][1]= cat
grid[1][1]= bat
Now, if I have the value cat
, could I retrieve those index i.e [0][1]
Upvotes: 0
Views: 49
Reputation: 2480
Yes. you can do by
for i in grid:
for j in i:
if grid[i][j] == 'cat':
print i, j
Output:
0 1
Upvotes: 1
Reputation: 832
You could iterate over all the elements like this:
def find(needle, hay):
for x in hay:
for y in x:
if hay[x][y] == needle: return x, y
return -1, -1
And then use this function
find('cat', grid)
Upvotes: 1