Behind the scene: Memory Address allocation and Copy/DeepCopy in Python with List

Lets say, in python list a=[1,1, 2]. Ever wondered how the memory allocation would happen in Python for the list elements ?

In Python, the variables are tagged, meaning the elements of above list 1,1 would be stored in the same location and 2 is stored in different location.

when we print the memory location of a i.e id(a) and  memory location of each elements , we get the results.

(4315182216, 140602806130168, 1) (4315182216, 140602806130168, 1) (4315182216, 140602806130144, 2)

As seen above the memory address of a = 4315182216, memory address of 1 is 140602806130168 for first and second elements.


Now, to see more understanding and impacts of above such memory allocation, we see the various ways of copying the list, and check the impact on copied list due to modification on the original list variable or list elements.


::Without using Copy Module::: i.e when b = a

----------------------------------------Memory addresses of List a, before modification on a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, before modification on a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of a[0]:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of list variable a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)

:: Using Copy Module:::  i.e when b = copy.copy(a)

----------------------------------------Memory addresses of List a, before modification on a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, before modification on a:----------------------------------------
(4315183944, 140602806130168, 1)
(4315183944, 140602806130168, 1)
(4315183944, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of a[0]:----------------------------------------
(4315183944, 140602806130168, 1)
(4315183944, 140602806130168, 1)
(4315183944, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of list variable a:----------------------------------------
(4315183944, 140602806130168, 1)
(4315183944, 140602806130168, 1)
(4315183944, 140602806130144, 2)

:: Using deepCopy Module:: i.e when b = copy.deepcopy(a)

----------------------------------------Memory addresses of List a, before modification on a:----------------------------------------
(4315183944, 140602806130168, 1)
(4315183944, 140602806130168, 1)
(4315183944, 140602806130144, 2)
----------------------------------------Memory addresses of List b, before modification on a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of a[0]:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)
----------------------------------------Memory addresses of List b, After Deletion of list variable a:----------------------------------------
(4315182216, 140602806130168, 1)
(4315182216, 140602806130168, 1)
(4315182216, 140602806130144, 2)

As seen above, in case of using of copy module the memory address of list variable b is changed to 4315182216 from 4315183944. Thats the reason any changes in the values and variables in the original list doesn't impact the copied list.  

Comments