Strange behavior of list.append method

I have an appiend list.append(value) and instead of appending the value, it changes all the list elements to the same value.

I have been working with python for a while and am thoroughly confused.

Does anyone have an ide how this could happen. I would be glad to furnish the code sample.

You’re going to need to show some code here, otherwise we’ll all just be guessing :slight_smile:

My guess is that you have a nested list structure, something like:

>>> inner = []
>>> inner
[]
>>> outer = [inner] * 4
>>> outer
[[], [], [], []]
>>> inner.append(1)
>>> outer
[[1], [1], [1], [1]]
>>> outer[0].append(2)
>>> outer
[[1, 2], [1, 2], [1, 2], [1, 2]]

In this particular example, inner.append (and outer[0].append, which is the same thing) appears to change everything in outer because outer contains 4 distinct references to inner, not 4 distinct list objects.