I'm trying to print a list of lists from a loop, but getting the wrong output! The last list that is appended to the bigger list is repeating.
The Output I'm expecting:
FINAL LIST:
[[(1, 2), (2, 3)],
[(2, 3), (3, 4)]]
The Output I get:
FINAL LIST:
[[(2, 3), (3, 4)],
[(2, 3), (3, 4)]]
What am I doing wrong here? Here's my code:
a = []
count = 1
#Function that generates some nos. for the list
def func():
del a[:]
for i in range(count,count+2):
x = i
y = i+1
a.append((x,y))
print '\nIn Function:',a #List seems to be correct here
return a
#List of lists
List = []
for i in range(1,3):
b = func() #Calling Function
print 'In Loop:',b #Checking the value, list seems to be correct here also
List.append(b)
count = count+1
print '\nList of Lists:'
print List
del a[:]does?Listcontains two references to the lista. Simply declareaas a local variable.del a[:]doesn't make any sense. It creates a new copy of the list and then immediately deletes it.del a[:]simply emptiesa. It's mutation, not reassignment.