You can use slicing for this:
def window(lst, index):
return lst[max(0,index-2):index+3]
For instance:
>>> for i in range(10):
print(i, window(list(range(10)), i))
0 [0, 1, 2]
1 [0, 1, 2, 3]
2 [0, 1, 2, 3, 4]
3 [1, 2, 3, 4, 5]
4 [2, 3, 4, 5, 6]
5 [3, 4, 5, 6, 7]
6 [4, 5, 6, 7, 8]
7 [5, 6, 7, 8, 9]
8 [6, 7, 8, 9]
9 [7, 8, 9]
" ", , .