0

I have 2 lists and I only want to update tempGrid, however whenever I do, my original Grid gets updated as well. I'm using copy() when assigning the tempGrid and I have verified that the id's of each list is different. For some reason tempGrid is still updating Grid.

    tempGrid = grid.copy()
    print(id(tempGrid), id(grid))
    while going:
        for i in range(len(grid)-1):
            for j in range(len(grid)-1):                    
                if grid[i][j] == 1:
                    if (grid[i-1][j] == 2) or (grid[i+1][j] == 2) or (grid[i][j+1] == 2) or (grid[i][j-1] == 2):
                        tempGrid[i][j] = 2
  • grid.copy() is a shallow copy what you want is a deep copy. Read more here (https://realpython.com/copying-python-objects/) – carlosdafield Nov 07 '21 at 00:14

0 Answers0