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

1

1 Answer 1

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)] 
Sign up to request clarification or add additional context in comments.

2 Comments

you don't need the forloop, this just initializes temps len(arr) number of times.
@ChristianSloper you're right but I assume this is only part of the code, considering the range(len(arr)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.