top of page

Shallow Copy/Deep Copy: you think you really know the difference?

  • Writer: Perry
    Perry
  • Oct 25, 2018
  • 2 min read

Those of you without a "Pythonic" way of thinking, who are confident that they know the difference between the two in Python, may say:

A shallow copy is made by copying the reference of the original object, so any changes to one impacts the other.

Now, what if I tell you while this may be true in some languages, it is NOT what we mean when we say shallow copy in Python? I admit that the Pythonic definition of shallow copy could be a bit confusing.

Let's first clarify some terminologies here: the term "copy" could be misleading, as we technically can use equals sign = to create a "copy" of the object (the so-called "shallow copy" by our "non-Pythonic" folks 😃)

This operation would create a copy (clone) of immutable objects only. For the case of mutable objects (like a list), although a new variable is created by this operation, it does not create a new object, and both old and new variables will share the reference of the old object. In Python, you may use the id() function to confirm this.


ree

Obviously, a better approach for copying an object would be in a way that the original object remains unchanged. Keep in mind though that you probably would not do this as often since copying is a costly process, ESPECIALLY in the context of BIG data!


Now, if = does not really create a "copy" of the object what does?

In Python, you could do this using a shallow copy or a deep copy.


"Pythonic" definition of shallow copy:

A shallow copy in Python would create a new object which stores the reference of the elements (children) of the object. This means that a shallow copy actually creates a "clone" if you have a flat object (this is where shallow copy definition in Python vs. elsewhere is different).


However, a shallow copy definition in Python matches with the shallow copy definition elsewhere if you are dealing with nested objects. Shallow copy in Python would not create a copy (clone) if you have nested objects. In other words, in the case of a nested object, shallow copy only copies the "reference" of the objects, which means that any change made to one of the objects (the original or the copy) would appear in the other one and vice versa.


Deep copy: The definition of a deep copy in Python is the same as the definition elsewhere. A deep copy does the copying process recursively. Therefore, in the case of a nested object, since the process is done recursively, all the elements of the lists within the parent lists are copied over. Copying an object using deep copy would walk the entire tree of the object to create an entirely independent clone of the object and all its children.


Side note: you could create a shallow copy for a list using the "copy" method as well, I have a tidbit ready here on my Insta related to this! #bigdataqueen 😎

In my recent post on insta, I am gonna cover the copy module in Python which is widely used to create a shallow copy (copy.copy()) or a deep copy (copy.deepcopy()).


Good Luck Data Scientists to Be! #python

Recent Posts

See All

Comments


© 2023 by Salt & Pepper. Proudly created with Wix.com

SUBSCRIBE VIA EMAIL

me_edited.jpg
bottom of page