Quick Python method to get neighbouring elements in 2D grid
Question: Is there a method somewhere in a Python package that returns the elements and/
or indexes of an element in a 2d grid. E.g. if we have:

    
    
    [[1, 2, 3, 4],
    
     [5, 6, 7, 8],
    
     [7, 8, 9, 0]]
    

..and we give the method the index `[0,1]` it should return `[1, 6, 3]` (if it
could return `[[0,0], [1,1], [0,2]]` that would be even better) and giving it
`[1,1]` would return `[5, 2, 8, 7]` (or the corresponding indexes- order isn't
important).

There is obviously the simple solution to this, however, it's too slow since I
want to do this on a large scale for arrays with several thousand elements.
Any suggestions? Thanks in advance.


Answer: Why is it too slow?

From the input coordinate [a, b] return the list [[a-1, b], [a+1, b], [a,
b-1], [a, b+1]], avoiding coordinates outside your grid.

