+ 2
A basic fundamental question regarding lists
First of all, lets look at variables instead of list for the sake of simplicity: Var1=1 Var2= Var1 Var1=3 print("Var1 :",Var1) print("Var2 :",Var2) >>>Var1 :3 >>>Var2 :1 # Logical and understood Now, if you perform similar operations on lists : List1 = [1,2,3,4] List2 = List1 List1[3]=5 print("List1 :",List1) print("List2 :",List2) >>> List1 :[1,2,3,5] >>> List2 :[1,2,3,5] Why does the 4th index also change in List2?
8 ответов
+ 2
I have made a little tutorial about it:
https://code.sololearn.com/c89ejW97QsTN/?ref=app
+ 3
List2= List1 says that both references to same object . As lists mutable making changes in one will effect other
https://code.sololearn.com/c55h227B4427/?ref=app
+ 2
Thanks a lot for the quick response guys.. Didn't know the forum was this active.. 😍😀
From your tutorial, what I understood is that it all boils down to how the objects in python are stored in memory. So, 1st 2 lines of my code in either case creates variables and Lists respectively, where the objects with same value are stored in the same place but with different nametags.
Now changes come due to 3rd line of code in each case. Variables being immutable, the 3rd line creates a different place in memory for Var1 since it's value has changed. However, assignment to List1 changes List2 because both are stored in the same place and a new place in memory is not assigned to List 2 (whose FAULT? 🤔..lists being mutable, of course😥)
+ 2
Just to make sure:
The second line does *not* create a new object.
var2 = var1 means: "The object known as var1 shall have the second name var2."
There's still only one object.
The 3rd line makes all the difference.
var1 = whatever means: 'The name var1, which was used for the object with the names var1 and var2, shall henceforth be used for something completely different.'
list1[3] = 5 is different. After this assignment, list1 *still* refers to the same list as list2!
Different would be if you wrote:
list1 = whatever.
In that case, it would work just as above, the name list1 being used for something new.
list1[3] = 5 means: "The slot list1[3], that held 4 before, shall instead hold 5 now."
Or, more precisely: "The 'name' list1[3] shall not be used for the object 4 anymore, but for the object 5."
+ 2
Yes, thats what I also meant -"the third line makes all the difference" 😀. Thanks for making it clear though 👍🤗
+ 2
Manish try something like this:
List1 = [1,2,3,4]
List2 = List1.copy ()
List1[3]=5
print("List1 :",List1)
print("List2 :",List2)
Adding "copy ()" to the list2 will not affect it when you change some values in the list1
Thus your output will be:
>>> List1 :[1,2,3,5]
>>> List2 :[1,2,3,4]
+ 1
Thank a lot Rex Josaphat ,HonFu and uday kiran. To conclude, I am just collating different methods (learnt from fellow Sololearners 😗 out here) to copy list without referencing to the same object 🧐:
1- List2 = list(List1)
2- List2 = List1.copy()
3- List2 = [ i for i in List1]
4- List2 = List1[:]
where List1 is the old list and List2 is the new *COPIED* list.
0
Because in list2 take a reference of list1 not a values