Here is a simplified example illustrating your problem.
Initialize a list object x
:
x = [0]
Initialize another list a
, storing the previous list x
in it as an element:
a = [x]
Initialize yet another list b
and add the elements from a
to it;
the only element in a
is the list object x
, which is now appended to b
:
b = []
b.extend(a)
This means that the first (and only) element in b
is that same list object x
;
not a clone, not another list with its elements, but that exact same object:
print(b[0] is x)
Gives True
.
Notice that I did not check equality, but identity using is
.
This means I can write
b[0][0] = 1
print(x[0] == 1)
and get True
.
Your code is a great example of why it is usually a terrible idea to muck around with global variables.