What is the Difference Between Shallow Copy and Deep Copy

The main difference between shallow copy and deep copy is that shallow copy creates a new object and then populates it with references to the child objects found in the original, while deep copy creates a new object and then recursively populates it with copies of the child objects found in the original.

In programming languages such as Python, we can use = operator to create a copy of an object. However, it does not create an object. It only creates a new variable that shares the reference of the original object. In programming, it might be required to have the original values unchanged and only modify the new values. There are two ways of creating copies in Python. Those are shallow copy and deep copy.

Key Areas Covered

1. What is Copy Module
      -Definition, Functionality
2. What is Shallow Copy
     -Definition, Functionality
3. What is Deep Copy
    -Definition, Functionality
4. Difference Between Shallow Copy and Deep Copy
     -Comparison of key differences

Key Terms

Copy module, Deep Copy, Shallow Copy 

Difference Between Shallow Copy and Deep Copy - Comparison Summary

What is Copy Module

For the shallow copying or deep copying to work, the program should consist of a copy module.  Programmer can use “import copy” module. Furthermore, copy() return a shallow copy of the object, while deepcopy() returns a deep copy of the object.

What is Shallow Copy

A shallow copy is a process of creating a new object which stores the reference of the original elements. It does not create copies of nested objects itself. An example is as follows.

Difference Between Shallow Copy and Deep Copy

Figure 1: Shallow Copy program with python

In the above program, the old_list is a nested list. Then, it is copied to the new_list using the copy() method. Likewise, it creates a new object with the same content. Therefore, when printing old_list and new_list, we can see the same output on the console.

Furthermore, in line 9, the programmer appends a new value to the first index element of the middle array. When printing the old_list and new_list again, we can see that element in both old_list and new_list gets modified because both lists share the reference of same nested objects.

What is Deep Copy

A deep copy is a process of creating a new object that recursively inserts the copies of nested objects of the original elements to the new object. In other words, a deep copy is capable of creating an independent copy of the original object and related nested objects.

Main Difference - Shallow Copy vs Deep Copy

Figure 2: Deep copy program with Python

In the above program, the old_list is a nested list. Then, it is copied to the new_list using the deepcopy() method. Likewise, it creates a new object with the same content. Therefore, when printing old_list and new_list, we can see the same output on the console.

Furthermore, in line 9, the programmer appends a new value to the first index element of the middle array. When printing the old_list and new_list again, we can see that only the old_list is modified. It is because they are separate objects.

Difference Between Shallow Copy and Deep Copy

Definition

Shallow copy is the process of constructing a new collection object and then populating it with references to the child objects found in the original. On the other hand, a deep copy is a process of constructing a new collection object and then recursively populating it with copies of the child object found in the original. Thus, this is the main difference between shallow copy and deep copy.

Functionality

Moreover, shallow copy does not recurse, and it does not create copies of the child objects themselves, while deep copy creates a complete independent clone of the original object and all of its children.

Copying process

Another difference between shallow copy and deep copy is that shallow copy is not recursive, whereas deep copy is recursive.

Conclusion

It is possible to copy objects in programming languages such as Python. There are two types of copying called shallow copy and deep copying. The main difference between shallow copy and deep copy is that shallow copy creates a new object and then populates it with references to the child objects found in the original, while deep copy creates a new object and then recursively populates it with copies of the child objects found in the original.

References:

1.“Python Shallow Copy and Deep Copy.” Python Shallow Copy and Deep Copy (With Examples), Available here.
2.Real Python. “Shallow vs Deep Copying of Python Objects.” Real Python, Real Python, 12 Dec. 2018, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply