Python issue with for loop and append

It's because when you append nima into mani, it isn't a copy of nima, but a reference to nima.

So as nima changes, the reference at each location in mani, just points to the changed nima.

Since nima ends up as [0, 1, 2], then each reference appended into mani, just refers to the same object.


Just to complete as some have suggested, you should use the copy module. Your code would look like:

import copy

mani=[]
nima=[]
for i in range(3):
    nima.append(i)
    mani.append(copy.copy(nima))

print(mani)

Output:

[[0], [0, 1], [0, 1, 2]]