Why is this happening?
Version 1:
mylist= [1,2,3,4,5]
print mylist
for index, value in enumerate(mylist):
value = 0
print mylist
Version 2:
mylist= [[1],[2],[3],[4],[5]]
print mylist
for index, value in enumerate(mylist):
value[0] = 0
print mylist
Output 1:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Output 2:
[[1], [2], [3], [4], [5]]
[[0], [0], [0], [0], [0]]
I assumed that both versions would make a local variable and do not overwrite the list itself. I guess that is not the case in second version. I'm using python 2.7. Clearly, I can have another variable inside of for loop that makes another copy of the value. It just took me some time to figure this out and it was messing up my code functionality.
Solution:
value = list(value)