A quick overview about shallow copy,deepcopy and assignment in python.

Copying objects can be done in three ways in Python:
- Assignment Operation
a=b
- Shallow copy
copy()a=copy(b)
- deepcopy
a=deepcopy(b)
Assignment Operation:
Assignment Operation in python usually won’t create copies. It will create references to the existing object.
- Immutable object
Example: int
,string
are immutable objects in python.

If the above example,if we assign x=y, both x and y will point to same object in memory.

If we try to modify the value of y=10
, only value of y
is changed. x
remains the same.y
will point to another int
object 10

https://gist.github.com/IndhumathyChelliah/5094c29169bbbd50c9b87eb9fbac1e83
Same for str
object also.
https://gist.github.com/IndhumathyChelliah/c06f618e448ab42d49b79be04c5a26ac
2. Mutable objects
Example: List,Set,Dictionary

Both x
and y
will point to same object in memory.

If we modify x
, bothx
and y
values are modified.
x.append(5)
Now also both x
and y
point to same List object in memory.

Example:
https://gist.github.com/IndhumathyChelliah/403ebba2033589771e5b01077941d470
Same for dict
object also.
https://gist.github.com/IndhumathyChelliah/ee2a5c4f3579bc0064c3082e139a5877
Shallow Copy:
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
Shallow copy can be done by copy() method.
copy()
To copy objects using copy()
method, we have to import copy module.
from copy import copy
Copying list object by using copy() function.

After copying list using copy()
function, both list x
and y
contains same elements.But both are different list objects. Their id
(memory location) is different.
https://gist.github.com/IndhumathyChelliah/cfaca7630abfdb88e0c1c7bd516e2412
But the nested list inside list x
and y
will point to same list object in memory.Memory location( id
) of nested list objects is same.
https://gist.github.com/IndhumathyChelliah/b42f5f938750f173e02530bba4f0abd7
Pictorial representation of list objects x
and y
in memory:

copy()
– It will create shallow copy of the list.Shallow copy means , it creates only outer list copy, not the nested list copy.Both nested list objects is same for both list x
and y
. If you try to modify the nested list then it will modify the original list too as the nested list object is same for both lists.
Example.
https://gist.github.com/IndhumathyChelliah/e8e705280ea041a827a7b2cbff0b716a
After modifying elements in the outer list, it is not reflected in the copied list.
Pictorial representation of list objects x
and y
after modifying elements in the outer list.

If we modify elements in the nested list ,it will get reflected in the copied list also. Because both nested list objects are pointing to same list object in memory. Shallow copy is only one level deep copy.
https://gist.github.com/IndhumathyChelliah/a7f220d9553f77817e08fdb04cd99d78
Pictorial representation of list objects x
and y
after modifying elements in the nested list.

Deepcopy:
A deepcopy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
To copy objects using deepcopy()
method, we have to import copy module.
from copy import deepcopy
Copying list object by using deepcopy() function.

After copying list using deepcopy()
function, both list x
and y
contains same elements.But both are different list objects. Their id
(memory location) is different.
https://gist.github.com/IndhumathyChelliah/c0586f2892b76b3f1fbcf9997e40feda
The nested list inside list x
and y
will also point to different list objects in memory.Memory location( id
) of nested list objects is also different.
https://gist.github.com/IndhumathyChelliah/b764c08956f120daf52d115b1f9dbeb8
Pictorial representation of list objects x
and y
in memory:

deepcopy()
– It will create deepcopy of the list.Deep copy means , it creates not only outer list copy, but also nested list copy.Nested list objects will be different for both list x
and y
. If you try to modify the nested list then it will not be reflected in the copied list ,because nested list objects is different for both lists.
After modifying elements in the outer list, it is not reflected in the deepcopied list.
https://gist.github.com/IndhumathyChelliah/d9f66609d0952a2b7ac34a61f9cd9de9
Pictorial representation of list objects x
and y
after modifying elements in the outer list.

If we modify elements in the nested list ,it will not get reflected in the copied list. Because both nested list objects are pointing to different list object in memory.
https://gist.github.com/IndhumathyChelliah/a5b5e5223924b4c2ecd102d712595b7c
Pictorial representation of list objects x
and y
after modifying elements in the nested list.

Conclusion:
deepcopy()
Copies everything.It copies object recursively.It means any changes made in the copy of object is not reflected in the original object
copy()
copy() function returns shallow copy of the object.If we modify elements in the outer list of copied list, it won’t be reflected in the original list.But if we modify nested list , it will be reflected in the original list.
Assignment Operation.
It won’t create copies.It creates only references to existing object.So If we modify immutable elements like int,str
in the copied object,it won’t be reflected in the original object.But if we modify mutable objects like list
, it will be reflected in the original object.
Resources(Python Documentation):
https://docs.python.org/3/library/copy.html?highlight=copy#copy.copy
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee