I was trying a program in which I declared a 2D array named grid of size 5 x 5 globally in python.
grid = [[0] * 5] * 5
def main():
    global grid
    n,m = map(int,input().split())
    for i in range(n):
        li = list(map(int,input().split()))
        for j in range(m):
            if j < 5:
                grid[i][j] = li[j]
    print(grid)
if __name__ == "__main__":
    main()
Modifying the grid gives a weird answer. For example if the input is :-
2 3
1 2 3
4 5 6
2 rows and 3 columns (n and m)
It gives the following output:-
[[4, 5, 6, 0, 0], [4, 5, 6, 0, 0], [4, 5, 6, 0, 0], [4, 5, 6, 0, 0], [4, 5, 6, 0, 0]]
Even when I remove the line global grid from the main function, output remains the same. What is going on? I want it to update grid according to the input and leave the extra cells untouched i.e value 0. If input is of size 2 x 3 then only the first 2 rows and 3 columns of the grid should be modified according to the input and rest of the elements should remain 0.
 
    