Best way not to run out of list index
<p>Let's say I have 2D list and I want to do a check if previous/next element equals something. What is the best way to make sure that I will not access <code>list[-1][-1]</code> or <code>list[len + 1][len + 1]</code>?</p>

<p>Here's an example of what I'm trying to do:</p>

<pre><code>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
</code></pre>

<p>I can't see any other options except for doing:</p>

<pre><code>if y - 1 &gt; 0 and y + 1 &lt; len(list) and x - 1 &gt; 0 and x + 1 &lt; len(list[y]):
</code></pre>

<p>Which doesn't seem right...</p>
