I create a list using the below codes.
a = [[False] * 3] * 3
which creates a 3x3 matrix with all elements of False
value.
And when I changed a[0][0]
to be True
using a[0][0] = True
, the result is:
[[True, False, False], [True, False, False], [True, False, False]]
where the entire first column changes into True
.
However, if I create a
using the for
loop, like:
a = [[False] * 3 for i in range(3)]
or
a = [[False for i in range(3)] for i in range(3)]
And after executing the same a[0][0] = True
, I get my desired result in both ways as:
[[True, False, False], [False, False, False], [False, False, False]]
where just one element is changed into True
.
Why there is such difference in creating the list in Python? Thank you for answering!