init = [[0,1],[10,11]]
constr1 = [init for _ in range(5)]
constr2 = init * 5
First question, why the above 2 constructed lists do not give the same result and they are instead
constr1
[[[0, 1], [10, 11]],
[[0, 1], [10, 11]],
[[0, 1], [10, 11]],
[[0, 1], [10, 11]],
[[0, 1], [10, 11]]]
constr2
[[0, 1], [10, 11],
[0, 1], [10, 11],
[0, 1], [10, 11],
[0, 1], [10, 11],
[0, 1], [10, 11]]
And the other question is if I edit the initial list
init[0][1]=2
then we can see that both constr1 and constr2 have been affected and they are
[[[0, 2], [10, 11]],
[[0, 2], [10, 11]],
[[0, 2], [10, 11]],
[[0, 2], [10, 11]],
[[0, 2], [10, 11]]]
and
[[0, 2], [10, 11],
[0, 2], [10, 11],
[0, 2], [10, 11],
[0, 2], [10, 11],
[0, 2], [10, 11]]
So what is the way to get a constructed list (by repetition of list init) and the constr list NOT being affected by any subsequent changes in the init list? NOTE I also tried the line (after clearing all my variables by restarting my python shell)
constr1 = [init[:] for _ in range(5)]
and still it is affected by a change in init[0][1]