You know, a tutorial on Python, we abuse this damn shallow copy and deep copy for multiple number
of times.
So in this video, we are going to discuss what is shallow copy and what is a deep copy.
So let us go for one practical demonstration to show you the differences between this shallow and the
deep copy.
So here is the demonstration in Python.
We use the equal to operator to create a copy of an object.
And we may think that that will create a new object, but it is not.
So it only creates a new variable that shows the difference of the original object.
Let us consider this example here.
So here you have defined one.
Least one, at least two is equal to least one here.
Do you see we have used the equal to operator.
So now what is happening?
You can find that if you print this list, one, if you print this list to getting the output like this.
So here is the respective output.
We are having this output like this.
Also, we are going to print the idea of at least one idea of these two.
You can find that both these two ideas are simple.
So that means here we are actually creating a new variable that will share the difference of the original
object.
So here the new variable is linked to an original object is our last one.
So now let us go for this list, one dot append.
So now here to see we are spending some new elements here.
So now we are going to print this list one and list two.
But we are finding that the new variables, whatever new values, whatever we are going to append is
getting reflected, is getting accessed from what, at least one analyst list to here.
So this is our that is a copy using our equal to operate.
So now we are going to discuss another two kinds of copy.
So there are two ways to create a copy.
One is the shallow copy and the one is a deep copy will be going to discuss on this very aspect now.
And to make this copy work, we use the copy model.
So that's why you have done the import copy here.
So here you are using this copy model and we use the copy model of Python for shallow and the deep copy
operations.
And suppose we need to copy the compound, say if we need to copy a complete list.
So in that case, it will be done in this way.
That is a copy copy or list and that will be assigned to the new list or later I can show you that will
be using copy dot deep copy onliest and that will be assigned to the new list.
So let us discuss one by one.
So at first we are going to discuss what is a shallow copy.
A shallow copy actually creates a new object which stores the reference of the original elements and
so, so is shallow.
Copy does not create a copy of the nested objects.
Instead it just copies the reforms of the nested objects.
That means that a copy process does not recurse or create copies of the NextEd objects itself.
It the things are not getting clear.
So let us go for one example here.
So here you have imported the copy model and all this is having the respective values.
New list is able to copy copy all.
Now here is the new list is the to copy that copy all list.
So here you are creating the copy, printing this all list, a new list as well.
So here is the respective output for us now, all this dot append.
So here you are, spending some new values to this, new elements to this list.
So now we are printing this list and the new list.
You can easily find that this new values are getting access to are getting referred by this list.
But the new list is only holding the references of the old values when the copy copy was issued.
So the new values are not getting access and are not getting an effort from this new list.
So now let us suppose let us do some adding that new nested objects, everything is in the shelf.
Copy here we are just doing some update on this item number one one with the valuate.
So that means there is a zero.
That is the one.
So one that is a zero element.
That is the one element.
So one man means this very element here.
So this very element in the list.
So it is getting updated by this eight.
And now in the new list, I'm getting this particular value here.
So now we can find out how these values are getting updated.
So just I'm just copying this one to show you that if you do the update.
At this particular place, the three one with the producer, Bebe.
Then obviously, if you print it, you can find that we're getting this bib here, but here we are not
getting this Beeby here and here to see we are going for this happened once again.
So Disvalue has got upended once again, but this baby has been upgraded in this all list.
But it is it is not there in the new year, so no send up operation will take place.
So in this way, we will discuss what is a shallow copy.
So actually a shallow copy does not create a copy of the nested objects.
Instead, it just copies the reference of the nested objects.
Only the difference of the nested objects will be copied.
So what is happening right now?
Nobody are doing some updates in this place.
You are getting the respective update in the list also.
So not let us go for the deep copy.
No question.
This coming in mind, what is a deep copy?
So in case of deep copy, what happens, I'm going to discuss in the details.
It creates a new object and recursively Airds the copies of the nested objects present in the original
elements.
So it's very important that in case of deep copy, we are creating a new object.
So obviously in this case, if we print their respective ideas, the ideas will be different here.
So let us go for the print here.
So I'm just going for this taking this copy.
So here to see we're having this list, having some set of elements, there's a new list, copy, dark,
deep copy or list.
So in this way, I'm just going for this.
I'm just printing it.
You can find that that new lists are getting printed.
So now I'm going for this idea of this new list.
And also the oldest.
So if you go for the print.
You're going to find that these two, or at least at least they're having their different IDs, so that
is it has created a new object for it.
So it creates a new object and recursively and the copies of the nested objects present in the original
elements.
And let's let us go for some updates here.
So now here is the oldest one zero is getting updated by the value.
So now you can find that we're having this update here.
But as in the new list, we're creating a new object.
I should not expect any update in this particular new list here.
So I think now the conception of shallow copy and the respective deep copy it even.
But he case of copy work or we're just calling the method there is a copy copy and include some deep
copy.
We're calling this method.
There is a copy, the deep copy.
And here in this particular demonstration, we have demonstrated that how this shallow copy and deep
copy works in our python coding.
Thanks for watching this video.