0

Why is 'New' not None? Why did 'Origin' replace all its values with new value of Row even though I have specified the position?

I thought 'New' will save the previous value of 'Origin' which is None, but when I use, it print the current value of 'Origin' ? Also why did new 'Origin' replace all its value with new value from 'Row' even though I have specified the position in it for replacement Im still new to python https://code.sololearn.com/ceKFIaSnXDUj/?ref=app

30th Sep 2020, 2:09 PM
MasterofBeginning
MasterofBeginning - avatar
3 Réponses
+ 2
To answer why New is not empty, and why all rows of Origin are the same, is basically the same idea. When you set New = Origin, they become two different names for the same list. So, if you change Origin in some way, you change New in the same way. See this code to see. a = [] b = a a.append(1) print(b) # [1] You set a = b, add an element to a, and print b to see that the same element is now in b. To see why Origin has the same line multiple times, look at lines 18 and 19. You are adding the same list to each place in Origin. To see how this works, see this code. a = [1,2] b = [a, a] # same list twice, b [[1,2], [1,2]] b[0].append(3) print(a) # [[1,2,3], [1,2,3]] Because you have the same list twice, changing the first means changing them both. Hope this helps
30th Sep 2020, 4:22 PM
Russ
Russ - avatar
+ 1
If you wanted to store the current value of Origin to the variable New, you could do it like this: def Create(a, b): global New .... New = Origin.copy() return Saving a copy of the list means that, although the New list is identical to Origin, it is not the *same* list. That means that changing Origin will not change New.
1st Oct 2020, 2:05 PM
Russ
Russ - avatar
0
So is there any way I can store the value of the 'Origin' list from function Create
1st Oct 2020, 1:45 PM
MasterofBeginning
MasterofBeginning - avatar