I am trying to understand why the following behaviour exists in Python.
If I make two equivalent matrices
matrix1 = [[False] * 3] * 3
matrix2 = [[False for x in range(3)] for y in range(3)]
Then I try to make the top left cell equal to True
.
matrix1[0][0] = True
matrix2[0][0] = True
Each array was changed in a different way.
matrix1 is now [[True, False, False], [True, False, False], [True, False, False]]
matrix2 is now [[True, False, False], [False, False, False], [False, False, False]]
matrix2
is the behaviour I was expecting. Why does matrix1
behave differently?