First, descriptive variable names help you understand what the code is doing. And using i, l, or O as single digit variable names can be confusing as the can look like numbers.
import pprint def change_list(board): for row_num in range(16): row=board[row_num] for column in range(16): if row[column] == 3: ## column before if column-1 >= 0 and board[row_num][column-1]==0: board[row_num][column-1]=3 ## column after if column+1 < 16 and board[row_num][column+1]==0: board[row_num][column+1]=3 ## row above if row_num-1 >= 0 and board[row_num-1][column]==0: board[row_num-1][column]=3 ## row below if row_num+1 < 16 and board[row_num+1][column]==0: board[row_num+1][column]=3 ## if you continue on the program will find the column+1 and their neighbors will ## be changed, and then the row+1 will be found, etc. until the end of the list return board board = [[0 for x in range (16)] for y in range(16)] board[5][2]=3 board=change_list(board) pprint.pprint(board)