0

I am trying to copy one 2D list to another 2D list and got unusual behaviour(from C++). Please help me out what exactly is happening here?

    rows, cols = (len(grid), len(grid[0]))
    arr = [[0]*cols]*rows
    
    for i in range(rows):
        for j in range(cols):
            arr[i][j] = grid[i][j]
    
    print(arr)

input :- grid = [[1,2,3],[4,5,6],[7,8,9]]
desired output :- print(arr) should print [[1,2,3],[4,5,6],[7,8,9]]
actual output :- [[7, 8, 9], [7, 8, 9], [7, 8, 9]]

0 Answers0