Reputation: 3
Thr version of Python is 2.6.6
n = 0
list1=["","",""]
list2=[]
for ... :
# a b c changes every loop
list1[0]=a
list1[1]=b
list1[2]=c
list2[n].append(list1)
n += 1
for j in range(n):
print list2[j]
The problem is that every item in list2 is the value of the last loop, Why? It seems caused by shallow copy, but i don't kown how to fix it.
Upvotes: 0
Views: 45
Reputation: 14738
You need to append a copy of list1
:
list2.append(list1[:])
Otherwise, what gets appended is a reference to the same single list.
Upvotes: 3