Based on your description of the symptom, I'm going to guess you defined the lists like this:
arr0, arr1 = [[None]]*2
In that case, both arr0
and arr1
refer to the exact same list, because [xyz]*2
produces the list [xyz, xyz]
where xyz
is the exact same object listed twice.
So when Python encounters [arr0[0], arr1[0]] = [10, 20]
it first assigns
arr0[0] = 10
and then
arr1[0] = 20
but since arr0
and arr1
both refer to the same list, the first element in both lists
is assigned-to twice:
In [46]: arr0, arr1 = [[None]]*2
Note that assignment to arr0
affects arr1
:
In [47]: arr0[0] = 10
In [48]: arr1
Out[48]: [10]
Note that assignment to arr1
affects arr0
:
In [49]: arr1[0] = 20
In [50]: arr0
Out[50]: [20]
Since the assignments are evaluated from left to right, the right-most assignment is last and so it is that assignment which ultimately affects both lists arr0
, arr1
.
To fix the problem: Use
arr0, arr1 = [None], [None]
or
arr0, arr1 = [[None] for i in range(2)]
since in both cases, arr0
and arr1
get assigned to different lists.