Jul-09-2020, 01:48 PM
x=[[0]*4]*10 creates the array[0, 0, 0, 0] and then makes an array that holds ten of these arrays. All of the arrays inside x are the same array. Not the same values (which they are), but the same array. It is as if you did this:
y=[0]*4 x=[y,y,y,y,y,y,y,y,y,y]A slightly shorter version of what you came up with:
x=[[0]*4 for _ in range(10)] x[0][0] = 1 print(x)
Output:[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], ...