I'm trying to get a list of indexes for a grid where the element is != 0 (the last part is not relevant to question however I thought I would add it)
#creating the grid
grid = [[0 for x in range(4)] for y in range(5)]
#taking each elements index
grid = [[[x,y] for x, Cell in enumerate(Row) if Cell == 0 ] for y, Row in enumerate(grid)]
print grid #indexes
[[(0, 0), (1, 0), (2, 0), (3, 0)], [(0, 1), (1, 1), (2, 1), (3, 1)], [(0, 2), (1, 2), (2, 2), (3, 2)], [(0, 3), (1, 3), (2, 3), (3, 3)], [(0, 4), (1, 4), (2, 4), (3, 4)]]
I now need to select a random index. I'm planning on using random.choice(), however because it's a list of lists this will be problematic. I realize I could just do two random.choice()s but i'd rather understand list comprehensions better.
Thanks
EDIT:
I was able to do it without a LC:
index = []
for y, Row in enumerate(grid):
for x, Cell in enumerate(Row):
if self.grid[y][x] == 0:
index.append((x,y))