Let's say I have a 2D list and I want to check if the previous / next element is equal to something. What is the best way to make sure that I don't get access to list[-1][-1] or list[len + 1][len + 1] ?
Here is an example of what I'm trying to do:
if list[y + 1][x] == value and list[y - 1][x] == value: do something elif list[y][x + 1] == value and list[y][x - 1] == value: do something else ... # end so on
I do not see any other parameters besides the following:
if y - 1 > 0 and y + 1 < len(list) and x - 1 > 0 and x + 1 < len(list[y]):
What doesn't seem right ...
source share