I'm currently working on a Python 3 project that involves iterating several times through a list of lists, and I want to write a code that skips over specific indices of this list of lists. The specific indices are stored in a separate list of lists. I have written a small list of lists, grid and the values I do not want to iterate over, coordinates:
grid = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
coordinates = [[0, 2], [1, 1], [2, 0]]
Basically, I wish to skip over each 1 in grid(the 1s are just used to make the corresponding coordinate locations more visible).
I tried the following code to no avail:
for row in grid:
for value in row:
for coordinate in coordinates:
if coordinate[0] != grid.index(row) and coordinate[1] != row.index(value):
row[value] += 4
print(grid)
The expected output is: [[4, 4, 1], [4, 1, 4], [1, 4, 4]]
After executing the code with, I am greeted with ValueError: 1 is not in list.
I have 2 questions:
Why am I being given this error message when each
coordinateincoordinatescontains a 0th and 1st position?Is there a better way to solve this problem than using for loops?