In Python, assignment statements ( =
) create bindings between a target and an object, rather than copying them.
A copy is sometimes required for mutable collections or collections that contain mutable elements, so that one copy can be changed without affecting the other. This module supports shallow and deep copy operations in general.
Ex.: With assignment operator
>>> a = [1,2,3] >>> b=a >>> print(a) [1, 2, 3] >>> print(b) [1, 2, 3]
Now if change element in list a, then it will be also make changes in list b, the reason behind it they both variable actually refer to the same memory location. Let’s see in example below.
>>> a[1]=6 >>> print(a) [1, 6, 3] >>> print(b) [1, 6, 3]
In sometimes, you may wish to leave the original values alone and only edit the new ones, or vice versa. There are two ways to make copies in Python:
Shallow Copy
Deep Copy
To make these copy work, we use the copy
module.
Shallow Copy
A shallow copy creates a new object that has the original elements’ references.
A shallow copy, on the other hand, does not generate a copy of nested objects; rather, it copies the reference to nested objects. This means that a copy operation does not recurs or duplicate nested items.
Ex.: Shallow copy with simple list
>>> import copy >>> lst1 = [1,2,3] >>> lst2 = lst1.copy() >>> print(lst1) [1, 2, 3] >>> print(lst2) [1, 2, 3]
Now changing the value of index[1] in lst2
>>> lst2[1]=5 >>> print(lst2) [1, 5, 3] >>> print(lst1) [1, 2, 3]
In above example lst1 & lst2 is not refer to a same memory location, it’s refer to different memory location this is why the both list variable has different list element. But when we talked about nested list the scenario will be different, Let’s see the example.
Ex. : Shallow copy nested list
>>> import copy >>> li1 = [[1,2],[3,4]] >>> li2 = li1.copy() >>> print(li1) [[1, 2], [3, 4]] >>> print(li2) [[1, 2], [3, 4]]
Now if i change the value of the nested list element it will reflected in both variable. Here i am changing the value of li2.
>>> li2[0][1]=5 >>> print(li2) [[1, 5], [3, 4]] >>> print(li1) [[1, 5], [3, 4]]
This happen because they both refer to the same objects that is present inside the nested list.
The other important thing is in shallow copy is that if i append the element into li1 it does not reflect the li2. Let’s see in example.
>>> li1.append([6,7]) >>> print(li1) [[1, 5], [3, 4], [6, 7]] >>> print(li2) [[1, 5], [3, 4]]
Here you can seen that [6,7] item is not appended in li2. this is because in shallow copy item will not copy but if you making a changes inside nested list it will reflect to the other list.
Deep Copy
The concept deep copy
refers to a recursive copying operation. It requires first creating a new collection object, then recursively populating it with copies of the original’s child objects. A copy of an object is copied into another object in a deep copy
.
Any modifications made to a copy of an object are not reflected in the original object. This is done in Python with the deepcopy()
method.
Ex.: Deep copy with simple list
>>> import copy >>> li1 = [1,2,3,4] >>> li2 = copy.deepcopy(li1) >>> print(li1) [1, 2, 3, 4] >>> print(li2) [1, 2, 3, 4]
If I make changes into li1 element it will behave same like a shallow copy.
>>> li1[1]=10 >>> print(li1) [1, 10, 3, 4] >>> print(li2) [1, 2, 3, 4]
Ex. : Deep copy nested list
>>> import copy >>> li1 = [[1,2],[3,4]] >>> li2 = copy.deepcopy(li1) >>> print(li1) [[1, 2], [3, 4]] >>> print(li2) [[1, 2], [3, 4]]
In above example deepcopy variable’s refer to the different memory location even in nested list. if i want to change some element inside the li1, it will not reflected in li2. Let’s see in example.
>>> li1[0][1]=100 >>> print(li1) [[1, 100], [3, 4]] >>> print(li2) [[1, 2], [3, 4]]
In above example you can see the difference in nested list in both shallow copy and deepcopy.
That is it for today, hope it helps.
If you have any suggestion for this article please make a comment in comment section below.
—
If you like this article, you can buy me a coffee. Thanks!