0

I was working in python on this problem: https://leetcode.com/problems/reshape-the-matrix/

I ran into issues because of this line [[0]*c]*r. Specifically when you edited on index in a row it would change the value at that index in every other row.

So if you said result = [[0]*3]]*2 and then tried to assign a value to result[1][1] = 42 then the matrix would look like [[0,42,0],[0,42,0]] instead of [[0,0,0],[0,42,0]]. I solved the problem by using a generator in a list, but I would still like to know why python has this behavior, and what is going on. Why does one of the *'s operate differently from the other? are they actually the same?

martineau
  • 112,593
  • 23
  • 157
  • 280
user11945129
  • 33
  • 1
  • 6

0 Answers0