Shallow vs Deep Copying of Python Objects
Table of Contents
Making Shallow Copies
Making deep copies, copying arbitrary python objects, 3 things to remember.
Assignment statements in Python do not create copies of objects, they only bind names to an object. For immutable objects, that usually doesn’t make a difference.
But for working with mutable objects or collections of mutable objects, you might be looking for a way to create “real copies” or “clones” of these objects.
Essentially, you’ll sometimes want copies that you can modify without automatically modifying the original at the same time. In this article I’m going to give you the rundown on how to copy or “clone” objects in Python 3 and some of the caveats involved.
Note: This tutorial was written with Python 3 in mind but there is little difference between Python 2 and 3 when it comes to copying objects. When there are differences I will point them out in the text.
Let’s start by looking at how to copy Python’s built-in collections. Python’s built-in mutable collections like lists, dicts, and sets can be copied by calling their factory functions on an existing collection:
However, this method won’t work for custom objects and, on top of that, it only creates shallow copies . For compound objects like lists , dicts , and sets , there’s an important difference between shallow and deep copying:
A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. In essence, a shallow copy is only one level deep . The copying process does not recurse and therefore won’t create copies of the child objects themselves.
A deep copy makes the copying process recursive . It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. Copying an object this way walks the whole object tree to create a fully independent clone of the original object and all of its children.
I know, that was a bit of a mouthful. So let’s look at some examples to drive home this difference between deep and shallow copies.
Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
In the example below, we’ll create a new nested list and then shallowly copy it with the list() factory function:
This means ys will now be a new and independent object with the same contents as xs . You can verify this by inspecting both objects:
To confirm ys really is independent from the original, let’s devise a little experiment. You could try and add a new sublist to the original ( xs ) and then check to make sure this modification didn’t affect the copy ( ys ):
As you can see, this had the expected effect. Modifying the copied list at a “superficial” level was no problem at all.
However, because we only created a shallow copy of the original list, ys still contains references to the original child objects stored in xs .
These children were not copied. They were merely referenced again in the copied list.
Therefore, when you modify one of the child objects in xs , this modification will be reflected in ys as well—that’s because both lists share the same child objects . The copy is only a shallow, one level deep copy:
In the above example we (seemingly) only made a change to xs . But it turns out that both sublists at index 1 in xs and ys were modified. Again, this happened because we had only created a shallow copy of the original list.
Had we created a deep copy of xs in the first step, both objects would’ve been fully independent. This is the practical difference between shallow and deep copies of objects.
Now you know how to create shallow copies of some of the built-in collection classes, and you know the difference between shallow and deep copying. The questions we still want answers for are:
- How can you create deep copies of built-in collections?
- How can you create copies (shallow and deep) of arbitrary objects, including custom classes?
The answer to these questions lies in the copy module in the Python standard library. This module provides a simple interface for creating shallow and deep copies of arbitrary Python objects.
Let’s repeat the previous list-copying example, but with one important difference. This time we’re going to create a deep copy using the deepcopy() function defined in the copy module instead:
When you inspect xs and its clone zs that we created with copy.deepcopy() , you’ll see that they both look identical again—just like in the previous example:
However, if you make a modification to one of the child objects in the original object ( xs ), you’ll see that this modification won’t affect the deep copy ( zs ).
Both objects, the original and the copy, are fully independent this time. xs was cloned recursively, including all of its child objects:
You might want to take some time to sit down with the Python interpreter and play through these examples right about now. Wrapping your head around copying objects is easier when you get to experience and play with the examples firsthand.
By the way, you can also create shallow copies using a function in the copy module. The copy.copy() function creates shallow copies of objects.
This is useful if you need to clearly communicate that you’re creating a shallow copy somewhere in your code. Using copy.copy() lets you indicate this fact. However, for built-in collections it’s considered more Pythonic to simply use the list, dict, and set factory functions to create shallow copies.
The question we still need to answer is how do we create copies (shallow and deep) of arbitrary objects, including custom classes. Let’s take a look at that now.
Again the copy module comes to our rescue. Its copy.copy() and copy.deepcopy() functions can be used to duplicate any object.
Once again, the best way to understand how to use these is with a simple experiment. I’m going to base this on the previous list-copying example. Let’s start by defining a simple 2D point class:
I hope you agree that this was pretty straightforward. I added a __repr__() implementation so that we can easily inspect objects created from this class in the Python interpreter.
Note: The above example uses a Python 3.6 f-string to construct the string returned by __repr__ . On Python 2 and versions of Python 3 before 3.6 you’d use a different string formatting expression, for example: Python def __repr__ ( self ): return 'Point( %r , %r )' % ( self . x , self . y ) Copied!
Next up, we’ll create a Point instance and then (shallowly) copy it, using the copy module:
If we inspect the contents of the original Point object and its (shallow) clone, we see what we’d expect:
Here’s something else to keep in mind. Because our point object uses immutable types (ints) for its coordinates, there’s no difference between a shallow and a deep copy in this case. But I’ll expand the example in a second.
Let’s move on to a more complex example. I’m going to define another class to represent 2D rectangles. I’ll do it in a way that allows us to create a more complex object hierarchy—my rectangles will use Point objects to represent their coordinates:
Again, first we’re going to attempt to create a shallow copy of a rectangle instance:
If you inspect the original rectangle and its copy, you’ll see how nicely the __repr__() override is working out, and that the shallow copy process worked as expected:
Remember how the previous list example illustrated the difference between deep and shallow copies? I’m going to use the same approach here. I’ll modify an object deeper in the object hierarchy, and then you’ll see this change reflected in the (shallow) copy as well:
I hope this behaved how you expected it to. Next, I’ll create a deep copy of the original rectangle. Then I’ll apply another modification and you’ll see which objects are affected:
Voila! This time the deep copy ( drect ) is fully independent of the original ( rect ) and the shallow copy ( srect ).
We’ve covered a lot of ground here, and there are still some finer points to copying objects.
It pays to go deep (ha!) on this topic, so you may want to study up on the copy module documentation . For example, objects can control how they’re copied by defining the special methods __copy__() and __deepcopy__() on them.
- Making a shallow copy of an object won’t clone child objects. Therefore, the copy is not fully independent of the original.
- A deep copy of an object will recursively clone child objects. The clone is fully independent of the original, but creating a deep copy is slower.
- You can copy arbitrary objects (including custom classes) with the copy module.
If you’d like to dig deeper into other intermediate-level Python programming techniques, check out this free bonus:
đ Python Tricks đ
Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
About Dan Bader
Dan Bader is the owner and editor in chief of Real Python and the main developer of the realpython.com learning platform. Dan has been writing code for more than 20 years and holds a master's degree in computer science.
Master Real-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
Whatâs your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal . Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session . Happy Pythoning!
Keep Learning
Related Topics: advanced python
Keep reading Real Python by creating a free account or signing in:
Already have an account? Sign-In
Almost there! Complete this form and click the button below to gain instant access:
"Python Tricks: The Book" â Free Sample Chapter (PDF)
đ No spam. We take your privacy seriously.
Learn Python practically and Get Certified .
Popular Tutorials
Popular examples, reference materials, learn python interactively, python introduction.
- Get Started With Python
- Your First Python Program
- Python Comments
Python Fundamentals
- Python Variables and Literals
- Python Type Conversion
- Python Basic Input and Output
- Python Operators
Python Flow Control
- Python if...else Statement
- Python for Loop
- Python while Loop
- Python break and continue
- Python pass Statement
Python Data types
- Python Numbers and Mathematics
- Python List
- Python Tuple
- Python String
- Python Dictionary
- Python Functions
- Python Function Arguments
- Python Variable Scope
- Python Global Keyword
- Python Recursion
- Python Modules
- Python Package
- Python Main function
Python Files
- Python Directory and Files Management
- Python CSV: Read and Write CSV files
- Reading CSV files in Python
- Writing CSV files in Python
- Python Exception Handling
- Python Exceptions
- Python Custom Exceptions
Python Object & Class
- Python Objects and Classes
- Python Inheritance
- Python Multiple Inheritance
- Polymorphism in Python
- Python Operator Overloading
Python Advanced Topics
- List comprehension
- Python Lambda/Anonymous Function
- Python Iterators
- Python Generators
- Python Namespace and Scope
- Python Closures
- Python Decorators
- Python @property decorator
- Python RegEx
Python Date and Time
- Python datetime
- Python strftime()
- Python strptime()
- How to get current date and time in Python?
- Python Get Current Time
- Python timestamp to datetime and vice-versa
- Python time Module
- Python sleep()
Additional Topic
- Precedence and Associativity of Operators in Python
- Python Keywords and Identifiers
- Python Asserts
- Python Json
- Python *args and **kwargs
Python Tutorials
Python List copy()
Python Dictionary copy()
Python Set copy()
Python List clear()
- Python Set union()
Python Shallow Copy and Deep Copy
Copy an object in python.
In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.
Let's take an example where we create a list named old_list and pass an object reference to new_list using = operator.
Example 1: Copy using = operator
When we run above program, the output will be:
As you can see from the output both variables old_list and new_list shares the same id i.e 140673303268168 .
So, if you want to modify any values in new_list or old_list , the change is visible in both.
Essentially, sometimes you may want to have the original values unchanged and only modify the new values or vice versa. In Python, there are two ways to create copies:
- Shallow Copy
To make these copy work, we use the copy module.
- Copy Module
We use the copy module of Python for shallow and deep copy operations. Suppose, you need to copy the compound list say x . For example:
Here, the copy() return a shallow copy of x . Similarly, deepcopy() return a deep copy of x .
A shallow copy creates a new object which stores the reference of the original elements.
So, a shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. This means, a copy process does not recurse or create copies of nested objects itself.
Example 2: Create a copy using shallow copy
When we run the program , the output will be:
In above program, we created a nested list and then shallow copy it using copy() method.
This means it will create new and independent object with same content. To verify this, we print the both old_list and new_list .
To confirm that new_list is different from old_list , we try to add new nested object to original and check it.
Example 3: Adding [4, 4, 4] to old_list, using shallow copy
When we run the program, it will output:
In the above program, we created a shallow copy of old_list . The new_list contains references to original nested objects stored in old_list . Then we add the new list i.e [4, 4, 4] into old_list . This new sublist was not copied in new_list .
However, when you change any nested objects in old_list , the changes appear in new_list .
Example 4: Adding new nested object using Shallow copy
In the above program, we made changes to old_list i.e old_list[1][1] = 'AA' . Both sublists of old_list and new_list at index [1][1] were modified. This is because, both lists share the reference of same nested objects.
A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements.
Letâs continue with example 2. However, we are going to create deep copy using deepcopy() function present in copy module. The deep copy creates independent copy of original object and all its nested objects.
Example 5: Copying a list using deepcopy()
In the above program, we use deepcopy() function to create copy which looks similar.
However, if you make changes to any nested objects in original object old_list , youâll see no changes to the copy new_list .
Example 6: Adding a new nested object in the list using Deep copy
In the above program, when we assign a new value to old_list , we can see only the old_list is modified. This means, both the old_list and the new_list are independent. This is because the old_list was recursively copied, which is true for all its nested objects.
Table of Contents
- Copy an Object in Python
Sorry about that.
Our premium learning platform, created with over a decade of experience and thousands of feedbacks .
Learn and improve your coding skills like never before.
- Interactive Courses
- Certificates
- 2000+ Challenges
Related Tutorials
Python Library
- Python »
- 3.12.5 Documentation »
- The Python Standard Library »
- Data Types »
- copy â Shallow and deep copy operations
- Theme Auto Light Dark |
copy â Shallow and deep copy operations ¶
Source code: Lib/copy.py
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations (explained below).
Interface summary:
Return a shallow copy of x .
Return a deep copy of x .
Raised for module specific errors.
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Two problems often exist with deep copy operations that donât exist with shallow copy operations:
Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.
Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.
The deepcopy() function avoids these problems by:
keeping a memo dictionary of objects already copied during the current copying pass; and
letting user-defined classes override the copying operation or the set of components copied.
This module does not copy types like module, method, stack trace, stack frame, file, socket, window, or any similar types. It does âcopyâ functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.
Shallow copies of dictionaries can be made using dict.copy() , and of lists by assigning a slice of the entire list, for example, copied_list = original_list[:] .
Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.
In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__() . The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the __deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument. The memo dictionary should be treated as an opaque object.
Discussion of the special methods used to support object state retrieval and restoration.
Previous topic
types â Dynamic type creation and names for built-in types
pprint â Data pretty printer
- Report a Bug
- Show Source
Shallow and deep copy in Python: copy(), deepcopy()
In Python, you can make a shallow and deep copy using the copy() and deepcopy() functions from the copy module. A shallow copy can also be made with the copy() method of lists, dictionaries, and so on.
- copy â Shallow and deep copy operations â Python 3.11.3 documentation
Shallow copy and deep copy in Python
Assignment to another variable, copy() method of list, dictionary, etc., list() , dict() , etc., copy.copy(), deep copy: copy.deepcopy().
The following is a summary of the differences between assignment to another variable, shallow copy, and deep copy.
The Python official documentation describes shallow copy and deep copy as follows:
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. copy â Shallow and deep copy operations â Python 3.11.3 documentation
When objects are contained within mutable objects, like elements in a list or values in a dictionary, a shallow copy creates references to the original objects, while a deep copy creates new copies of the original objects. With references, the elements point to the same object, so modifying one of them also affects the other.
First, let's explore what happens when assigning an object to another variable.
When a mutable object, such as a list or a dictionary, is assigned to multiple variables, updating one variable (e.g., changing, adding, or deleting elements) will also affect the other variables.
As demonstrated in the above code example, the is operator shows that the two variables refer to the same object both before and after the value change.
- Difference between the == and is operators in Python
To create a copy instead of a reference of the same object, use the copy() method or the copy.copy() and copy.deepcopy() functions described below.
For immutable objects like numbers ( int , float ) and strings ( str ), they cannot be updated. When these types of objects are assigned, the two variables refer to the same object initially. However, if one variable is updated to a new value, it becomes a separate object, and the other variable remains the same.
Shallow copy: copy() , copy.copy() , etc.
The copy() method is provided for lists, dictionaries, etc. The copy() method makes a shallow copy.
A shallow copy creates a new object that contains references to the elements found in the original object. For example, in the case of a list, the copied list is a new object, but its elements are references to the same objects in the original list.
Therefore, if the elements are mutable, updating one also affects the other. However, in the case of an immutable element, when its value is changed, it becomes a separate object. The corresponding element in the other list remains unchanged.
The same applies not only to a list of lists as in the example above, but also to a list of dictionaries, a nested dictionary (a dictionary containing other dictionaries as values), and so on.
Slices for mutable sequence types, such as lists, also make shallow copies.
- How to slice a list, string, tuple in Python
For example, applying the slice [:] that specifies all elements makes a shallow copy of the whole list.
Before Python 3.3 introduced the copy() method for lists, the [:] notation was commonly used to create a shallow copy. For new code, it is generally recommended to use the copy() method to make your intentions clearer.
A slice of a part of the sequence also creates a shallow copy.
If you need to make a deep copy, you can apply the copy.deepcopy() function to the slice.
You can make a shallow copy of a list or dictionary by passing the original list or dictionary to list() or dict() .
You can also make a shallow copy with the copy() function from the copy module.
Use copy.copy() when you need to create a shallow copy of an object that does not provide a copy() method.
To make a deep copy, use the deepcopy() function from the copy module.
In a deep copy, actual copies of the objects are inserted instead of their references. As a result, changes to one object do not affect the other.
Here's an example of applying the deepcopy() function to a slice:
Related Categories
Related articles.
- NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan()
- Generate gradient image with Python, NumPy
- Filter (extract/remove) items of a list with filter() in Python
- Method chaining across multiple lines in Python
- Draw circle, rectangle, line, etc. with Python, Pillow
- Python, Pillow: Flip image
- pandas: Apply functions to values, rows, columns with map(), apply()
- Use enumerate() and zip() together in Python
- How to use range() in Python
- Save frames from video files as still images with OpenCV in Python
- pandas: Sort DataFrame/Series with sort_values(), sort_index()
- Reverse a list, string, tuple in Python (reverse, reversed)
- Convert a string to a number (int, float) in Python
- Copy a file/directory in Python (shutil.copy, shutil.copytree)
- Remove an item from a dictionary in Python (clear, pop, popitem, del)
Deep vs Shallow Copies in Python
- Introduction
In this tutorial, we are going to discuss shallow copies vs deep copies with the help of examples in Python. We will cover the definition of a deep and shallow copy, along with its implementation in the Python language to evaluate the core differences between the two types of copies.
In many of the programs that we write, no matter how basic they are, we end up needing to copy a list or an object for one of many reasons, like computational efficiency. There are two ways to do that, either make a deep copy or a shallow copy. Before we discuss the differences between the two, let's first understand what deep and shallow copies exactly are.
- Deep Copies in Python
A deep copy makes a new and separate copy of an entire object or list with its own unique memory address. What this means is that any changes you make in the new copy of the object/list won't reflect in the original one. This process happens by first creating a new list or object, followed by recursively copying the elements from the original one to the new one.
To put it briefly, both of the objects are completely independent of each other. This is similar to the concept of passing by value in languages like C++, Java, and C#.
- Deep Copy Example
To implement the concept of deep copies in Python, we'll be using the copy module.
Let's say we have a list of lists called result_A , which contains grades of student A in 3 subjects for the first two years of school, and we wish to create an exactly similar list of results for student B as well. We'll try and make a deep copy of the result_A list, and make a few changes in that deep copy later to show the grades of student B.
In the script above, we use the deepcopy method from the copy module to copy list result_A to result_B . Next, we print the contents of both the lists on the screen.
As you can see, the lists are identical. Later on in this article we'll see how this is different from shallow copies.
- Shallow Copies in Python
A shallow copy also makes a separate new object object or list, but instead of copying the child elements to the new object, it simply copies the references to their memory addresses. Hence, if you make a change in the original object, it would reflect in the copied object, and vice versa. To put it briefly, both the copies are dependent on each other. This is similar to the concept of passing by reference in programming languages like C++, C#, and Java.
- Shallow Copy Example
To implement this in Python, we'll use the copy module again, but this time we'll be calling its copy function.
Let's use the same example list for the shallow copy example as well.
In the script above, we use the copy method from the copy module to make a shallow copy of list result_A which we named result_B . Next, the contents of both the lists have been printed on the console.
Again, the lists are the same, as expected. Next we'll explain the difference between the results we get from the copy and deepcopy functions.
- Difference Between Deep and Shallow Copies
Now that we have discussed what shallow and deep copies are and why we create copies, it's time to talk about the difference between them. Essentially, there are just two core differences and they're linked with each other:
- Deep copy stores copies of an object's values , whereas shallow copy stories references to the original memory address
- Deep copy doesn't reflect changes made to the new/copied object in the original object; whereas, shallow copy does
Before we move on to the implementation, I'd like you to imagine this scenario. Let's say two people wish to share a drink; they have two empty glasses and two straws. They can share this drink in two ways:
- Put the drink in one glass, and put both the straws in that glass for sharing
- Put the drink in both the glasses, and put one straw in each glass
The first scenario is that of a shallow copy. Both the variables/instances are pointing/using the same memory location for their operations. The second scenario is that of a deep copy. Both variables/instances are pointing to/using two different memory locations for their operations.
- Comparison Example
To make the difference clear, let's use this information in our above two examples, starting with Example 1 .
Above, we created the list result_A and made a deep copy of it named result_B . Let's try to change the content in result_B and see if it has any effect on the contents of result_A .
The expected outcome is that the original list remains unchanged. And as you can see, changes in the deep copy didn't affect the original list.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Now, lets try the same thing with Example 2 - Shallow Copy.
Here the expected outcome is that both the original list and copied list are modified after the single change. And as you can see, making the change to the shallow copy resulted in that change being reflected in the original list as well.
In this post, we talked about what a shallow and a deep copy is and how we can make them in Python language using the 'copy' module. We used two of its functions i.e. copy and deepcopy to make shallow and deep copies, respectively. Furthermore, we discussed the two core differences between a shallow and a deep copy, and also implemented shallow and deep copy in python to understand those differences in a better way.
You might also like...
- Hidden Features of Python
- Python Docstrings
- Handling Unix Signals in Python
- The Best Machine Learning Libraries in Python
- Guide to Sending HTTP Requests in Python with urllib3
Improve your dev skills!
Get tutorials, guides, and dev jobs in your inbox.
No spam ever. Unsubscribe at any time. Read our Privacy Policy.
In this article
Monitor with Ping Bot
Reliable monitoring for your app, databases, infrastructure, and the vendors they rely on. Ping Bot is a powerful uptime and performance monitoring tool that helps notify you and resolve issues before they affect your customers.
Vendor Alerts with Ping Bot
Get detailed incident alerts about the status of your favorite vendors. Don't learn about downtime from your customers, be the first to know with Ping Bot.
© 2013- 2024 Stack Abuse. All rights reserved.
DEV Community
Posted on Feb 3, 2020 • Updated on May 3, 2020 • Originally published at sharmapacific.in
Assignment vs Shallow Copy vs Deep Copy in Python
This article was originally shared on my blog .
Today, we will be discussing the copy in Python. There are three ways we can do it. In this article, you will get to know what each operation does and how they are different.
1 - Assignment Operator (=) 2 - Shallow Copy 3 - Deep copy
Assignment Operator (=)
In the above example of the assignment operator, It does not make a copy of the Python objects instead it copying a memory address (or pointer) from a to b , (b=a) . Which means both a & b pointing to the same memory address.
Here we can use the id() method to get the address of the object in memory and check if both lists are pointing the same memory.
So, here if you would edit the new list, Itâll get updated in the original list also -
Because there is only one instance of that list in the memory.
Shallow Copy
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
we have 3 different ways to create a shallow copy -
Here all the above lists contains the same values as the original list -
but the memory address of each is different -
This means that this time, each list's object has its own, independent memory address.
Now move to the more interesting part. If the original list is a compound object (e.g. a list of lists), then after shallow copy new list elements are still referencing the original elements. So, if you modify the mutable elements like lists, the changes will be reflected on the original elements. Let's look at the below example to get a better understanding -
As you see in the above example while we are modifying the internal list elements in the new list, itâs getting updated in the original list also, because a[0] and b[0] are still pointing to the same memory address(original list).
So the new list b has its own memory address but its elements do not. because in shallow copy instead of copying the list's elements to the new object, It simply copies the references to their memory addresses, Therefore while we are making changes to the original object it's reflecting in copied objects and vice versa.
This is a characteristic of shallow copy.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Creating a deep copy is slower because you are making new copies for everything. In this rather than just copy the address of the compound objects, It Simply makes a full copy of all the list's elements (simple and compound objects) of the original list and allocates different memory address for the new list and then assigns them the copied elements.
To achieve the deep copy we have to import the copy module. And use copy.deepcopy() .
As you see above the original list does not get affected.
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances).
Hope you like the explanation of Assignment Operator, Shallow Copy and Deep Copy. Still, if any doubt or improvement regarding Copy in Python, ask in the comment section.
References: https://docs.python.org/2/library/copy.html
Top comments (0)
Templates let you quickly answer FAQs or store snippets for re-use.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .
Hide child comments as well
For further actions, you may consider blocking this person and/or reporting abuse
Understanding Java Memory Leaks and How to Prevent Them
Isaac Tonyloi - SWE - Sep 1
CONFIGURE EXPRESS WITH .EJS TEMPLATE
Stillnoturdad - Sep 1
Creating safe and fast multiplayer in games on Unity and NodeJS with examples
Devs Daddy - Sep 1
DOM Manipulation: Selecting and Manipulating DOM Elements
Srijan Karki - Aug 9
We're a place where coders share, stay up-to-date and grow their careers.
21.13 â Shallow vs. deep copying
While this code looks harmless enough, it contains an insidious problem that will cause the program to exhibit undefined behavior!
Deep copying
When the overloaded assignment operator is called, the item being assigned to may already contain a previous value, which we need to make sure we clean up before we assign memory for new values. For non-dynamically allocated variables (which are a fixed size), we donât have to bother because the new value just overwrites the old one. However, for dynamically allocated variables, we need to explicitly deallocate any old memory before we allocate any new memory. If we donât, the code will not crash, but we will have a memory leak that will eat away our free memory every time we do an assignment!
Differences Between a Deep Copy and a Shallow Copy
Last updated: March 18, 2024
- Programming
It's finally here:
>> The Road to Membership and Baeldung Pro .
Going into ads, no-ads reading , and bit about how Baeldung works if you're curious :)
1. Introduction
In this article, we’re going to look at some of the ways we can safely share the same data between different areas of code , both by literally sharing the exact memory and by making copies of it as appropriate.
2. References vs Values
In many languages, such as Java, most of our variables do not store the actual value but instead store a reference, or pointer, to the value :
There are some significant benefits to working this way. For example, when we pass variables around, we’re only passing the small reference instead of the larger value around.
This can also allow many different variables to point to the exact same value in memory:
This can be useful because it means that both variables will see the exact same data. However, it also means that if one of them changes, then the other will automatically see the same changes at the same time. The two variables are always the same.
Note that this only applies when using Objects. Primitives – like int  and byte  – are always stored and passed as the exact value and not a reference to the value. This is fine because the largest primitive – long – is typically the same amount of memory as the reference would be, and primitives are always immutable, so they can’t be changed anyway.
3. What Is a Shallow Copy?
In some cases, we may want to create a copy of a value so that two different pieces of code see different copies of the same value. This allows one to be manipulated differently from the others, for example.
The simplest way to do this is to make a shallow copy of the object. This means we create a new object that contains all the same fields as the original, with copies of the same values:
For relatively simple objects, this works fine. However, if our objects contain other objects, then only the reference to these will be copied. This, in turn, means that the two copies contain references to the same value in memory, with the pros and cons that this entails:
In this example, both our original and our copy have a field “def” that points to the same list of numbers. If one of them changes the list, the other will see the same changes. However, because we’ve made a copy of the original, it might be surprising that the underlying data is still shared between them, and this can lead to unexpected bugs in our code.
4. What Is a Deep Copy?
The alternative to this is to perform a deep copy of the object. This is where we copy each field from the original to the copy, but as we do so, we perform a deep copy of those instead of just copying the references :
This will then mean that the new copy is an exact copy of the original, but in no way connected so that no changes to one will be reflected in the other.
5. Immutability vs Copying
The main benefit of making copies of our data is that two different pieces of code can act on it without interference. If we have two pieces of code that are each given the exact same list, and one removes an item from it, then the other will see that change as well. Making a copy of the list means that changes to one are not seen on the other.
However, copying objects can be expensive. The more complicated the object structure, the more expensive it can be. And in some cases, copying might be impossible – for example, if the object represents a physical resource such as a network socket or a file handle, instead of just some computer memory.
However, there’s another alternative. If our objects are immutable – that is, the values can never be changed – then there is much less risk in sharing the exact same values between different pieces of code. If we pass our list around to different pieces of code, but we can guarantee that it will never change, then we know that this will be safe.
However, writing immutable code isn’t always easy, especially with nested structures. For example, we might have an object that only has getters and no setters – so its fields can never be changed. This object is, in itself, immutable, but if any of those fields are themselves mutable, then the same problems can arise:
In this example, it’s impossible to change the names field in our object. It will always point to the same list. However, what happens here?
Even though our names field can never be changed, we’ve still managed to insert a new entry into it. And this entry will be seen by both immutable and immutable2 at the same time because they both point to the same memory.
6. Copy-on-Write
In some cases, we want to have values that are mutable but we don’t want to pay the cost of copying them if we don’t need to. In this case, we can use a pattern called Copy-on-Write. In this case, we create a copy of our object that points to the original. However, we will then make a copy of the original as soon as we want to make any changes to it:
Here, our CopyOnWrite class wraps an instance of our Original class. This means that the exact same value can be shared around cheaply. However, the first time we call setValue() on our wrapper, we immediately stop and make a local copy of the original.
At this point, we’re paying the cost to perform the deep copy, but it means that our changes are local only to this instance and not seen in any other instances.
Here, we’ve seen some ways that we can share data between different areas of our code and explored some of the ways that this can be done so that one area can’t inadvertently affect the other.
- C++ Classes and Objects
- C++ Polymorphism
- C++ Inheritance
- C++ Abstraction
- C++ Encapsulation
- C++ OOPs Interview Questions
- C++ OOPs MCQ
- C++ Interview Questions
- C++ Function Overloading
- C++ Programs
- C++ Preprocessor
- C++ Templates
Shallow Copy and Deep Copy in C++
In general, creating a copy of an object means to create an exact replica of the object having the same literal value, data type , and resources. There are two ways that are used by C++ compiler to create a copy of objects.
- Copy Constructor
- Assignment Operator
Depending upon the resources like dynamic memory held by the object, either we need to perform Shallow Copy or Deep Copy in order to create a replica of the object. In general, if the variables of an object have been dynamically allocated, then it is required to do a Deep Copy in order to create a copy of the object but one may wonder what is the shallow copy and deep copy? Don’t worry, GeeksforGeeks got you covered.
In this article, we will learn what is shallow copy, what is deep copy, how they are different from each other, where they happen by default and where we need one over another.
What is Shallow Copy?
In shallow copy, an object is created by simply copying the data of all variables of the original object. This works well if none of the variables of the object are defined in the heap section of memory but if some variables are dynamically allocated memory from heap section, then the copied object variable will also reference the same memory location.
This will create ambiguity and run-time errors, dangling pointer. Since both objects will reference to the same memory location, then change made by one will reflect those change in another object as well. Since we wanted to create a replica of the object, this purpose will not be filled by Shallow copy.Â
Note: C++ compiler implicitly creates a copy constructor and overloads assignment operator in order to perform shallow copy at compile time.
Consider the below objects B1 and B2, having integer data members representing dimensions of a 3d box.
Now imagine one of the members is a pointer to the integer variable. Here, the shallow copy will only copy the address stored in the pointer leading both the object member pointing to the same object.
Below is the implementation of the above example:
What is Deep Copy?
In Deep copy, an object is created by copying data of all variables, and it also allocates similar memory resources with the same value to the object. In order to perform Deep copy, we need to explicitly define the copy constructor and assign dynamic memory as well, if required. Also, it is required to dynamically allocate memory to the variables in the other constructors, as well.
Consider the previous example again. If we deep copy the object, then each object will have their own copy of the data pointed by the breadth pointer.
Difference between the Shallow Copy and Deep Copy
The below table list the differences between the shallow copy and the deep copy in the tabular form:
 S.No. | ||
---|---|---|
When we create a copy of object by copying data of all member variables as it is, then it is called shallow copy | When we create an object by copying data of another object along with the values of memory resources that reside outside the object, then it is called a deep copy | |
A shallow copy of an object copies all of the member field values. | Â Deep copy is performed by implementing our own copy constructor. | |
In shallow copy, the two objects are not independent | It copies all fields, and makes copies of dynamically allocated memory pointed to by the fields | |
It also creates a copy of the dynamically allocated objects | If we do not create the deep copy in a rightful way then the copy will point to the original, with disastrous consequences. |
Please Login to comment...
Similar reads.
- cpp-constructor
- Top Android Apps for 2024
- Top Cell Phone Signal Boosters in 2024
- Best Travel Apps (Paid & Free) in 2024
- The Best Smart Home Devices for 2024
- 15 Most Important Aptitude Topics For Placements [2024]
Improve your Coding Skills with Practice
What kind of Experience do you want to share?
Java Tutorial
Control statements, java object class, java inheritance, java polymorphism, java abstraction, java encapsulation, java oops misc.
IMAGES
VIDEO
COMMENTS
465 Normal assignment operations will simply point the new variable towards the existing object. The docs explain the difference between shallow and deep copies: The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
In Python 3, you can use list.copy(). However, I prefer the equivalent expression list[:] because it works in both Python 2 and 3. Shallow copy is different from assignment in that it creates a ...
What's the difference between a shallow and a deep copy of a Python object? Learn how to clone arbitrary objects in Python, including your own custom classes.
Deep copy doesn't reflect changes made to the new/copied object in the original object. Shallow Copy stores the copy of the original object and points the references to the objects. Deep copy stores the copy of the original object and recursively copies the objects as well. A shallow copy is faster. Deep copy is comparatively slower.
1. This is slightly misleading. Both a shallow and deep copy will copy the object to a new location in memory, a deep will also copy the child objects whereas a shallow will just have the new objects refer to the old children. It's difficult to read without referring to the original object.
In this article, you'll learn about shallow copy and deep copy in Python with the help of examples.
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. Two problems often exist with deep copy operations that don't exist ...
This article describes what will happen in memory when we assign a variable, shallow copy it, or deep copy it. Differences between assignment, shallow copy, and deep copy were summarized in a table.
Learn how to use copy module in Python to create deep and shallow copies of objects, and avoid unwanted changes in your data structures.
In Python, you can make a shallow and deep copy using the copy() and deepcopy() functions from the copy module. A shallow copy can also be made with the copy() method of lists, dictionaries, and so on. The following is a summary of the differences between assignment to another variable, shallow copy, and deep copy.
Introduction In this tutorial, we are going to discuss shallow copies vs deep copies with the help of examples in Python. We will cover the definition of a deep and shallow copy, along with its implementation in the Python language to evaluate the core differences between the two types of copies.
Understanding the differences between assignment, shallow copy, and deep copy is vital.
Hope you like the explanation of Assignment Operator, Shallow Copy and Deep Copy. Still, if any doubt or improvement regarding Copy in Python, ask in the comment section.
Shallow copying. Because C++ does not know much about your class, the default copy constructor and default assignment operators it provides use a copying method known as a memberwise copy (also known as a shallow copy). This means that C++ copies each member of the class individually (using the assignment operator for overloaded operator=, and ...
Differences Between a Deep Copy and a Shallow Copy. 1. Introduction. In this article, we're going to look at some of the ways we can safely share the same data between different areas of code, both by literally sharing the exact memory and by making copies of it as appropriate. 2.
Shallow Copy and Deep Copy in C++. In general, creating a copy of an object means to create an exact replica of the object having the same literal value, data type, and resources. There are two ways that are used by C++ compiler to create a copy of objects. // Default assignment operator. Depending upon the resources like dynamic memory held by ...
When we create a copy of the DataFrame using shallow copy ( DataFrame.copy(deep=False)), we are creating a copy with shared data and index. What this means is that we will be creating two separate objects, but the index and data will be shared. So just like in the case of =, any modifications to the shallow copy will affect the original ...
Shallow Copy vs Deep Copy in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods ...
So basically, shallow copy performs deep copy on value type variables by default and reference type variable is pointing to the original objects reference memory location, whereas deep copy allocates new memory for whole object ( including reference type variables ).
1. I have heard about the difference between shallow copy and assignment in python, "A shallow copy constructs a new object, while an assignment will simply point the new variable at the existing object. Any changes to the existing object will affect both variables (with assignment)" In the example below, I have a class contains 2 variables ...
Slicing makes a shallow copy of a list, not a deep copy - but none of your lists have nested lists (or other containers), so there is no difference between a shallow and a deep copy.