Copy in Python – Python Deep Copy and Shallow Copy

1. Copy in Python

Today, in this Python Tutorial, we will see the copy in Python. Moreover, we will define Deep Copy and Shallow Copy in Python. Also, we will see a difference between Shallow Copy vs Deep Copy. Along with this, we will discuss Copy Module and Shallow Copying Dictionaries.

So, let’s start Copy in Python.

Copy in Python - Shallow Copy and Deep Copy

Copy in Python – Shallow Copy and Deep Copy

2. Python Copy Module

When we perform an assignment in Python, it does not copy the object we assign. All it does is create bindings between a target and an object. But sometimes, we may need to change one Python copy without changing the other fora mutable collection.
In this Python Copy tutorial, we discuss the module copy in Python. It has the following members-

  • copy.copy(x)

This returns a shallow copy of x.

  • copy.deepcopy(x)

This returns a deep copy of x.

  • exception copy.error

Do you know about Python Decision Making Statements
This is the exception it raises for module-specific errors.

Before we can begin explaining shallow copy and deep copy, we think it is necessary to tell you these concepts apply to compound objects only- those that hold other objects like lists or class instances.

3. Python Deep Copy

When a deep copy in Python creates a new object, it inserts into the new object copies of the objects in the original object. In other words, it copies an object into another. This means any changes we make to the copy do not reflect in the original.

a. Deep Copy Example

Let’s try implementing this in Python. We use the deepcopy() function.

>>> import copy
>>> list1=[1,3,[7,4],6]
>>> list2=copy.deepcopy(list1) #Making a deep copy
>>> list1

[1, 3, [7, 4], 6]

>>> list2

[1, 3, [7, 4], 6]
Have a look at Python Syntax and Semantics

>>> list2[2][0]=5 #Modifying the element at index 2,0
>>> list1

[1, 3, [7, 4], 6]

>>> list2

[1, 3, [5, 4], 6]
As you can see, this did not change anything for the original object.

b. Problems with Python Deep Copy

  • It is possible that recursive objects cause a recursive loop; these are compound objects that directly or indirectly reference themselves.
  • It is possible that a deep copy may copy too much.

To deal with these problems, deepcopy():

Join DataFlair on Telegram
  • Keeps a memo dictionary of objects is copied during the current copying pass.
  • Allows user-defined classes to override the copying operation or the copied component set.

4. Python Shallow Copy

With a Shallow Copy in Python, we create a new object of which we recursively put copies of objects into the original. In other words, we copy a reference of an object into another. Any changes we make to the copy do reflect in the original.
Do you know about Python Function Arguments
Let’s implement this with Python. We’ll use the copy() function.

>>> import copy
>>> list1=[1,3,[7,4],6]
>>> list2=copy.copy(list1) #Making a shallow copy
>>> list1

[1, 3, [7, 4], 6]

>>> list2

[1, 3, [7, 4], 6]

>>> list2[2][0]=5 #Modifying the element at index 2,0
>>> list1

[1, 3, [5, 4], 6]

>>> list2

[1, 3, [5, 4], 6]
It is apparent that making changes to a shallow copy does change the original object.

Python Interview Questions

5. Shallow Copying Dictionaries

Let’s first create a dictionary in Shallow Copy in Python.

>>> dict1={'a':1,'b':2,'c':[1,2,3]}

Now, we’ll make a copy of it.
Let’s revise the Python Built-in Function

>>> dict2=dict1.copy()

Finally, we’ll append a new element to this.

>>> dict2['c'].append(7)
>>> dict1

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}

>>> dict2

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}
Other ways to create a shallow copy of a dictionary are dict(dict1) and copy.copy(dict1). As you can see, altering the mutable in the copy changed the original too. To prevent this, we can do a deep copy in Python.

>>> import copy
>>> dict1={'a':1,'b':2,'c':[1,2,3]}
>>> dict2=copy.deepcopy(dict1)
>>> dict2['c'].append(7)
>>> dict1

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3]}

>>> dict2

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}
So, this was all in Copy in Python. Hope you like our explanation of Shallow Copy and Deep Copy.
You must read Python Multiple Inheritance

6. Conclusion

Hence, today, in this Copy in Python Tutorial, we discussed Shallow copy and Deep copy with Python. Where shallow copy changes reflect in the original object, deep copy changes don’t. Moreover, we understood a difference between Shallow Copy vs Deep Copy in Python. Also, we saw Shallow Copy Dictionaries. Still, if any doubt regarding Copy in Python, ask in the comment tab.
See also –
Python Date and Time Modules
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.