0
for i in range(len(arr)):
     temps = [[0]*9]*3   
# temp = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 
0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]

when tries

temps[0][0] = 4

gives this output

[[4, 0, 0, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0, 
0, 0], [4, 0, 0, 0, 0, 0, 0, 0, 0]]

tries to change the value of temp[0][0] it changes all temp[0][0] temp[0][1] temp[0][2] value

Shah Vipul
  • 625
  • 7
  • 11
  • How did you initialize the `temp` list? See here: https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – Andrej Kesely Oct 07 '20 at 08:50

1 Answers1

1

Christian is right. [t] * 3 is equivalent to [t, t, t]

You can solve it by doing this:

for i in range(len(arr)):
     temps = [[0]*9 for _ in range(3)] 
Jasmijn
  • 9,370
  • 2
  • 29
  • 43