Python offers several efficient approaches to managing data. Understanding shallow and deep copy concepts is crucial when working with data structures like nested lists, dictionaries, or custom objects.

Both shallow and deep copy let you make replicas of data structures, but they act differently regarding nested data.

changes to nested list of shallow copy

Using Shallow Copy

Shallow copy works by creating a copy of the top-level structure of the original object. This means that, if the original object contains nested objects, the copy will reference the same nested objects that the original does. In other words, making a shallow copy of an object duplicates its outermost structure, not any nested objects it may contain.

To perform a shallow copy in Python, you can use the copy module’scopy()function or the.copy()method on the object.

changes to outer items of shallow copy

Consider an example ofworking with a list or dictionary in Python.

In the code above, themain_listvariable holds a list containing integers and an inner list (nested object) containing letters. The copy function creates a copy of themain_listwhich the code stores in another variable,shallow_copy.

using shallow copy with nested dictionary

Any changes you make to theshallow_copynested list will also directly affect that of themain_listand vice versa. These changes show that the nested or inner list of theshallow_copyis just a reference to that of themain_list, making the changes apply inmain_listtoo.

Meanwhile, any changes made to the outer items (the integers) in eithershallow_copyormain_listwill only affect that instance. These outer items are independent values in their own right, not just mere references.

using deep copy with nested list

The output demonstrates that both list’s outer items are independent of each other:

The same idea applies when working with dictionaries.

Changes made to the nested dictionary ofdict1affect bothdict1anddict2. At the same time, changes to the outer items ofdict1affect only it.

Using Deep Copy

Instead of referencing the nested objects of the original copy, a deep copy makes an entirely separate copy of the original object and its nested objects. Modifying the deep copy will not affect the original object and vice versa; they are truly separate values.

To make a deep copy in Python, use thedeepcopy()function of the copy module.

using shallow copy with  custom object

Consider an example of working with a list.

Here, the code performs a deep copy ofmain_list, creating an independent copy nameddeep_copy.

When you modify the nested list or outer items in thedeep_copy, your changes do not affect the original list, and vice versa. This demonstrates that the nested list or outer elements are not shared between the two copies.

Working With Custom Objects

you may create a custom object bydefining a Python classand creating an instance of the class.

Here’s an example of creating a simple object from aBookclass:

Now, make both a shallow copy and a deep copy of an instance of thisBookclass using thecopymodule.

As you can see, the shallow copy (book2) is a new object, but it references the same inner object (author list) as the original object (book1). Hence, a change to the original object’s authors affects both instances (book1 and book2), while a change to the outer item (price) only affects the original object (book1).

On the other hand, making a deep copy creates an independent copy of the original object, including copies of all objects contained within it.

In this case, the deep copy (book2) is a completely independent object, and modifying the original object (book1) does not affect it.

Uses for Shallow Copy and Deep Copy

It’s vital to understand deep and shallow copy so you can select the appropriate approach for manipulating data. Here are some scenarios where each method is applicable:

Performance and Considerations

Since shallow copy doesn’t generate new instances of nested objects, it typically runs faster and uses less memory than deep copy. However, the original and the shallow copy may have unwanted side effects from changing shared internal items.

Particularly for big and deeply nested data structures, deep copy,a recursive procedure, can be slower and use more memory. However, it ensures total independence between the original and the deep duplicate, making intricate data manipulation more secure.

The Best Copy Option for Your Data

Many programming languages use the concept of shallow and deep copy. Understanding it lets you manipulate data without unforeseen consequences.

By using shallow and deep copy techniques, you can select the best approach to duplicate your data structures safely. By understanding the effects on your data, you’ll get more dependable and predictable outcomes from your code.