I was working with matrices in python, and after hours spent trying to debug a program, managed to trace the problem to essentially this code, where all non-zero entries of a matrix are uniformly increased.
list2=[[1,2],[0,4]]
list1=list2
for row in list1:
for i in range(0,len(row)):
if row[i]!=0:
row[i]=row[i]+10
print(list1) #returns [[11,12],[0,14]], as expected
print(list2) #returns [[11,12],[0,14]], want to return [[1,2],[0,4]]
There's something fundamental I'm missing here. I thought by declaring list1=list2
a new list was created, which the rest of the code modified while keeping list2
unaltered.
What's the problem, and how do I fix it?