0

I am trying to reshape the matrix but when I execute following code I see that the assignment statement changes assign values to the entire row, why it does not assign the value to the only cell it should be

mat = [[1,2],[3,4]]
r = 4
c = 1

m = len(mat)
n = len(mat[0])
            
return_mat = [[0]*c]*r  
rrow = 0
rcol = 0
        
for row in range(m):
    for col in range(n):
        return_mat[rrow][rcol] = mat[row][col]
        rcol += 1
                
        if rcol == c:
            rcol = 0
            rrow = rrow + 1

in the above code, when the statment return_mat[rrow][rcol] = mat[row][col] executes it changes values for entire row

the final output comes like this: [[4],[4],[4],[4]]

can you please tell me why is the behavior like this?

dhandai
  • 69
  • 1
  • 2
  • 16
  • 3
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – 0x5453 Dec 06 '21 at 21:38

0 Answers0