title: Pythonic and efficient way of finding adjacent cells in grid
body: <p>I am building a tile based app in Python using pyglet/openGL wherein I'll need to find the all of the adjacent cells for a given cell. I am working in one quadrant of a Cartesian grid. Each cell has an x and y value indicating it's position in the grid( x_coord and y_coord ). These are not pixel values, rather grid positions. I am looking for an efficient way to get the adjacent cells. At max there are eight possible adjacent cells, but because of the bounds of the grid there could be as few as 3. Pseudo-code for a simple yet probably inefficient approach looks something like this: </p>
  
  <pre><code>def get_adjacent_cells( self, cell ):
       result = []
       x_coord = cell.x_coord
       y_coord = cell.y_coord
       for c in grid.cells:
            if c.x_coord == x_coord and c.y_coord == y_coord: # right
                 result.append( c )
            if c.x_coord == x_coord - 1 and c.y_coord == y_coord + 1: # lower right
                 result.append( c )
            if c.x_coord == x_coord - 1 and c.y_coord == y_coord: # below
                 result.append( c )
            if c.x_coord == x_coord - 1 and c.y_coord == y_coord - 1: lower left
                 result.append( c )
            if c.x_coord == x_coord and c.y_coord == y_coord - 1: right
                 result.append( c )
            // -- similar conditional for remaining cells
  </code></pre>
  
  <p>This would probably work just fine, though it is likely that this code will need to run every frame and in a larger grid it may affect performance. Any ideas for a more streamlined and less cpu intensive approach? Or, should I just roll with this approach?</p>
  
  <p>Thanks in advance.</p>