Difference Between Shallow Copy, Deep Copy And Normal Assignment Operation?

In this post, we will learn What is the difference between shallow copy, deep copy, and normal assignment operation where there are three different ways to make copies of the objects and here we will discuss the differences between all of them.

Shallow

Object Copies

There are different ways where we can make copies of Objects and here we are going to discuss all of them along with there examples where how we can create copies.

The copy could be different things in python like variables, objects or constructor or functions so we have explained here different ways to deal with it and how we can copy certain thing we wantto.

Shallow Copy

In a shallow copy of copying the object, we create a new object as a copy but the new objects have references to the older or the last object where we created the object originally. This indicates a reference here that if we make any of the changes in the original object the copied will also be affected means the changes will be reflected in the new object also and vice versa. This makes shallow a special copy technique to get a more clear idea and have an implementation have a look at the code given below.

import copy

list1 = [1, 2, [3, 4]]
list2 = list1.copy()
list2[2][0] = 5

print(list1)  # Output: [1, 2, [5, 4]]

And here we have made a change in list2 but the changes will be reflected in both the lists, list1 as well as in list2.

Deep Copy

In this case of object copy, we create a new object and recursively copy the object. That simply means when we make changes in any of the objects or list the changes will not be reflected in another or we can say the changes in one object will be limited to that object only it is not going to reflect in another object.

import copy

list1 = [1, 2, [3, 4]]
list2 = copy.deepcopy( list1)
list2[2][0] = 5

print(list1)  # Output: [1, 2, [3, 4]]

Here first we copied the object from one to another and then made some modifications in one of the objects but in the result, we can see that the changes we made are limited to the object in which we made the changes it did not affect the elements of another object.

Normal Assignment

In this case, we create a new object and share the same location we simply copy the object with the help of the ‘=’ operator and in this case, also the changes made in one are reflected in another object as a shallow copy so the example of the same is given below where we can see how does it worked and get it done by implementing the same as it is in your projects:

list1 = [1, 2, 3]
list2 = list1
list2.append(4)

print( list1)  # Output: [1, 2, 3, 4]

Here also we created a list list1 and copy the object to another which is list2 with the help of the ‘=’ operator after that copy, we simply make the same modifications in it as we are checking for whether it will be reflected in another object too or not and the result we see that the modification we made is also reflected in another object too.

 

 

To learn more about What is the difference between shallow copy, deepcopy, and normal assignment operation visit: the  by stack overflow

To learn more about Python solutions to different Python problems and tutorials for the concepts we need to know to work on Python programming along with different ways to solve any generally asked problems: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?.

Leave a Comment

%d bloggers like this: