If I understand correctly, I think you want:
temp = [l[y-1:y+2] for l in grid[x-1:x+2]] # extract local data
temp[1][1] = "@" # replace centre
walls = sum(l.count("#") for l in temp) # count walls
The slices you are trying to use:
l[n::3]
would take every third item from index n
of l
to the end; the arguments to slices are, as with range
, [start:stop:step]
.