Here I take care of clearing the code:
Edit: I took David code in my answer to make it even more compact (and faster runtime).
>>> from itertools import product >>> >>> def fringe8((px, py), (x1, y1, x2, y2)): ... f = [(px+dx, py+dy) for (dx, dy) in product((-1,0,1),(-1,0,1)) if (dx, dy) != (0, 0)] ... f_inrange = [(fx, fy) for (fx, fy) in f if x1 <= fx < x2 and y1 <= fy < y2] ... return f_inrange ... >>> fringe8((2, 2), (1, 1, 3, 3)) [(1, 1), (1, 2), (2, 1)]
Edit: If you are not too comfortable understanding the list, feel free to break it down into a for loop. The conditions offered here and the answers of others are more understandable to you.
After all, the goal of achieving a list is to simplify reading, not verbose.
Edit again: Also see Ryan Ginstrom's answer .