Python

Python Deep Copy

You may have gone through the concept of copying files and folders from one place to another within your specific operating system. This operation is quite easy as it doesn’t require you to perform some code. Just like file systems, programming also supports copying different objects. Python language also lets us copy one object to another using deep copy, shallow copy, and assignment methods. A deep copy is the type of copy in which the properties of a replica object don’t use similar references as the properties of an original object occupies.

In other words, the newly made object copy will separately occupy its sources and properties and will be completely independent of the original object. Within this guide today, we will be discussing the use of deep copy in python code to copy one object to the same type of another object. Before that, we need to install Python’s new version on our Linux system so that we can implement python examples. Use the beneath “apt” instruction with the “python3” keyword to configure it fully. Add your system account password and hit the key “Enter” to complete it.

You need to quickly open up your Linux shell with the “Ctrl+Alt+T.” We will be starting from the making of a “copy.py” python file. We have utilized the Linux “touch” command on the command-line shell. This will be created in 3 seconds, and we can open it with any editor like vim, text, and nano. We are opening our new python file in the “Gnu Nano” editor to create the python document following the shown-below query.

Example 01: Simple/Shallow Copy Using Assignment Operator

We will be starting our first example of copy using the simple method that is quite popular among different developers. This method uses the simple assignment operator “=” to copy the object to another. There will be no use of any built-in method to copy an object. After the empty python file is launched in the nano shall, we have added python-support at its first line. After this, the print statement states that the object will be displayed before any update. A list “l1” is initialized with numbers and printed out using the print statement. We have copied the contents of list l1 to another list l2 using the assignment operator “=.” The next 2 print statements display the second list after copying, and the third shows that we are going to perform updates. We have replaced the value at index 5 of list 2 with another value. After that, we have displayed both lists, l1, and l2, again. The last 2 print statements display the “ids” of both lists separately. Save the code with Ctrl+S and exit with Ctrl+X.

We have executed our python file “copy.py” with python3. Both the lists have been displayed before updating, i.e., the same values. After adding value at index 5 of list 2, we printed out both lists again. The display of both lists shows that the change in the replica list is updating the original list, i.e., the same updated lists displayed. Also, the IDs of both lists are the same, which means both are using the same resources via references.

Example 02: Deep Copy

To use the deep copy concept in python, we must utilize the “copy” module in our code file. This copy module will use its built-in functions to perform the deep copy in python. So, we have updated our old python file a little bit. Added the python extension and imported the “copy” module using the keyword “import.” Use a different file name and not like “copy.py” as it will be problematic and make errors. We have initialized a list l1 with different numerical and string values after the print statement states that we will perform a deep copy.

The list has been displayed using the print clause. To use the deep copy() function to copy list l1 to list l2, we need to use the module “copy” as a keyword with a dot before the function call. Within the deepcopy() function parameters, we have been taking list l1. The assignment operator is used to add the copy into list l2. After performing a deep copy, the print statement is again used to display list l2 on the shell. After all this, we have been appending two values in list l1 using the append() function of python. The last 2 print statements are used to display the contents of both the lists: l1 and l2 once again.

After running this updated code, we have got the two lists displayed at the start after performing the deep copy. While appending 2 new values to the first list, we have displayed both lists again. The display of both lists shows that the update in the original list doesn’t cause any change in list2. This indicates that the deep copy will create completely different resources from the source of the original object to be used for a replica. This is why the update in one doesn’t change the other.

Example 03: Deep Copy vs Shallow Copy

To demonstrate the deep copy in-depth, we will be comparing it with the shallow copy concept. So, we have updated our code and initialized a nested list l1 after importing the “copy” module, i.e., list within a list. To perform shallow copy, we have been using the copy() function of module “copy” and using list l1 to make a new list l2.

Both lists have been printed out using print clauses. We have updated the value of list 2 at index 1 of its list index 1. Again used the print statements to see the changes in both lists.

We have got the list l1 and l2 displayed after shallow copy. On updating list l2, we have again displayed both lists and found that a change in one is causing the other to change.

We have updated the code again and changed the text in the first print statement to “Deep Copy.” Performed the deep copy using the deepcopy() function of the “copy” module and copied the contents of a list l1 to new list l2. Printed out both lists at separate lines. Performed the update in list l2 by changing the value at index 1 of its list index 1 and displayed both lists again.

Using the deepcopy() function, we have copied list l1 to new list l2. After the update, we know that change in one nested object doesn’t affect the other object.

Conclusion:

This is all about using the deep copy concept to create a deep copy of one object in Python using the “copy” module of python. We have discussed this concept thoroughly using the deepcopy() function and discussed the simple copy method of python before. Also, we have compared the deep copy concept with a shallow copy in the Python example. This has been done to make it more clear.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.