I am trying to create a list of size n with each element also being a list of size m. After the creation of the list, I perform the following operation.
matrix=[[0]*m]*n
# matrix = [[0 for i in range(0,m)] for j in range(0,n)]
m=2
n=3
ind=[[0,1][1,1]]
for r,c in ind:
for i in range(m):
matrix[r][i]+=1
for j in range(n):
matrix[j][c]+=1
print(matrix)
The above operation looks for the row and column value in the list "ind" and increments the value by one of the corresponding row and column.
The problem is when I am using the matrix initialization as [[0]*m]*n then the result is coming different and when I am using matrix = [[0 for i in range(0,m)] for j in range(0,n)] the result is coming correct. In the first case the values get changed for all the rows, columns which shouldn't be the case.
I tried doing the type of the individual elements of the matrix list as well as getting the type of the element of the matrix element using print(type(matrix[0])) and print(type(matrix[0][0]))
Both have the same type. Any idea why the result of the matrix is coming different for the 2 different type of initialization?
Thanks for the help.