Just rearrange the directions to give mostly right order for lists (if it is sensitive which order they are). Take out the stupid bug I did in hurry to def getneigh(sh,i,j,d=directions), and try some loops over required directions. The neighbours=dict() is of course one demonstration of building list/dict by getneigh function, same results could have done to do printing instead of putting to dict in first loop.

Notice if you want to have only one direction (singleton tuple) you must use form (down,) comma after the value to signify tuple not normal brackets.

This function works with the original shape of yours also.

def getneigh(shape,i,j,d=directions):
    res=[]
    lenr=len(shape[0])
    numr=len(shape)
    for a,b in d:
        x,y=a+i,b+j
        if (shape[i][j] in 'x+-' and
##        if (shape[i][j] in 'x' and
            0<=x<numr and 0<=y<lenr):
            sym=shape[x][y]
            if sym in 'x+-':
                if sym == 'x': sym='RC'
                res.extend([sym, (x,y)])
    return res
shape=[\
    '          ',
    ' ++++     ',
    ' xxxx     ',
    ' xxxx     ',
    ' xxxx     ',
    ' xxxxxxxx ',
    ' xxxxxxxx ',
    ' xxxxxxxx ',
    ' xxxxxxxx ',
    '     xxxx ',
    '     xxxx ',
    '     xxxx ',
    '     ---- ',
    '          ']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.