+ 2

How copying of lists work in Python?

>>>a=[2,3,4] >>>b=a >>> # now a and b, both refers same memory address >>># check memory addresses with print(hex(id(a))) and print(hex(id(b))) >>>del a >>>b [2,3,4] So where does this variable [2,3,4] actually stored? Explain, please.

12th Apr 2017, 6:55 AM
Chetan Vashistth
Chetan Vashistth - avatar
3 odpowiedzi
+ 8
The list is stored until at least one variable points to it. The del command just destroys one of the pointers. The list itself is stored in yet another place - try to print hex(id([2,3,4])) along with those of a and b, to see that.
12th Apr 2017, 8:30 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
notable point of list copying is list by default follows shallow copy. here is code you can check and understand. https://code.sololearn.com/cDssM174GLKf/?ref=app
12th May 2018, 9:29 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
Actually python has automatic memory management. => Automatic memory management: If the values of python variables are same then it allocates a single memory block. so if you have delete one obj , other is still pointing to same memory location.
12th May 2018, 9:27 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar