Shallow vs Deep Copy

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 126:-  Shallow vs Deep Copy

Shallow copy and deep copy are concepts used when dealing with objects and data structures in programming, particularly in languages that support complex data structures and object-oriented programming. They refer to how data is duplicated or shared between original and copied objects. Let's explore both concepts:

Shallow Copy: A shallow copy creates a new object but does not create copies of nested objects contained within the original object. Instead, it copies references to those nested objects. As a result, changes made to nested objects in the copy are reflected in the original object and vice versa.

In a shallow copy, only the top-level structure is duplicated, and any deeper levels still share the same underlying data.

Shallow copy is typically faster and requires less memory, but it can lead to unintended side effects if the original and copied objects are expected to be independent.

Deep Copy: A deep copy, on the other hand, creates a completely independent copy of the original object, including copies of all nested objects within it. Changes made to the copied object or its nested objects do not affect the original object, and vice versa.

Deep copy ensures a true separation between the original and copied objects, preventing unintended interactions. However, it can be slower and consume more memory, especially for complex and nested data structures.

Here's a Python example to illustrate the difference between shallow copy and deep copy using the copy module:

pythonCopy code

import copy # Original list with nested lists original_list = [[1, 2, 3], [4, 5, 6]] # Shallow copy shallow_copy = copy.copy(original_list) # Deep copy deep_copy = copy.deepcopy(original_list) # Modifying nested list in the shallow copy shallow_copy[0][0] = 100 # Modifying nested list in the deep copy deep_copy[0][1] = 200 # Print original, shallow copy, and deep copy print("Original List:", original_list) print("Shallow Copy:", shallow_copy) print("Deep Copy:", deep_copy)

Output:

luaCopy code

Original List: [[1, 100, 3], [4, 5, 6]] Shallow Copy: [[1, 100, 3], [4, 5, 6]] Deep Copy: [[1, 200, 3], [4, 5, 6]]

As you can see, the changes made to the nested lists in the shallow copy affect the original list, while changes in the deep copy do not affect the original list.

In summary, shallow copy shares references to nested objects, while deep copy creates independent copies of both the top-level object and all its nested objects. The choice between shallow copy and deep copy depends on the specific requirements of your program and the desired behavior of the copied data structures.

17. OOPs

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?