I've encountered what I think is odd behaviour for the append()
function, and I've managed to duplicate it in the following simplified code:
plugh1 = []
plugh2 = []
n = 0
while n <= 4:
plugh1.append(n)
plugh2.append(plugh1)
n = n+1
print plugh1
print plugh2
I would expect this code to result in:
plugh1 = [1, 2, 3, 4]
plugh2 = [[1], [1, 2], [1, 2, 3, ], [1, 2, 3, 4]]
but the actual result is:
plugh1 = [1, 2, 3, 4]
plugh2 = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
As the loop runs, each time all array elements are replaced with the value of plugh1.
There is a similar question on SO but the solution seems related to nesting functions and defining a variable outside of those calls. This is much simpler I think. What am I missing?