I am facing an issue that i cannot solve on my own right now. Its about the following snippet:
counter = 0
appendList = []
valueList = [[0], [0]]
for i in range(0,3):
valueList[1] = counter
print "Loop " , i , " valueList: " , valueList
print "Appending (valueList): " , valueList , " to (appendList): " , appendList
appendList.append(valueList)
counter = counter + 1
print "Final appendList: " , appendList
This results in the following output:
Loop 0 valueList: [[0], 0]
Appending (valueList): [[0], 0] to (appendList): []
Loop 1 valueList: [[0], 1]
Appending (valueList): [[0], 1] to (appendList): [[[0], 1]]
Loop 2 valueList: [[0], 2]
Appending (valueList): [[0], 2] to (appendList): [[[0], 2], [[0], 2]]
Final appendList: [[[0], 2], [[0], 2], [[0], 2]]
I wanted the Snippet to add different List-Items to the appendList. The final result should have looked like this:
[[[0], 0], [[0], 1], [[0], 2]]
But as you can see, the snippet fills the appendList with the same values of the highest counter.
Can someone please explain this behavior to me or tell me, where my mistake is?
appendList.append(valueList[:])