Python List (Part IV)

Vivek Nikam and Abhay Chaudhari
4 min readJun 12, 2021

smart and simple

Photo by Nick Morrison on Unsplash

So, I hope you got the concepts which we have discussed in the last articles

Now you are familiar with the list and functions in the list

Let’s move to understand important concepts in the list

  1. Shallow copy
  2. Deep copy

_________________________________________________________________

Let’s understand the actual problem first

suppose I have A = [10, 20, 30] and I am assigning list A to B

B = A

here you can see B will also point to the same object

If I add or remove any value from A that will also be removed or added in B

means any change in A will reflect in B

let’s add 40 to list A and print B

So here we can say that this is inconsistent because we are not adding anything in B still B is getting updated.

To handle such kind of situation we use the concept of shallow copy and deep copy

_________________________________________________________________

  • Shallow copy:

we already know that we can have a list inside another list

A = [10, 20,[30, 40]]

len(A) will be 3 here because the inner list will be treated as a single object.

let’s see in detail

if we print A, B, and C

C is the shallow copy of A which means now C is physical a different copy of A. Change in A will not reflect in C.

let’s see whether it is true or false

Ok so now it is fine that changes in A not reflecting in C

but let’s see by adding an item in the sublist

oops, in this situation it is affecting list C

why it is so?

let’s understand it diagrammatically

when we perform C = A.copy( ), it creates a separate physical object and will refer to the objects to which A is referring. i.e 10, 20, and object of sublist, keep in mind we are referring to sublist object, not actual 30 and 40.

When we are changing the value of A[0] the reference of that object will get removed from A and B but not from C

but when we are adding or deleting any value from the sublist it will reflect in C because we are referring object of the sublist not the items in the sublist.

So now we can say that any change in the mutable object of the list will reflect in shallow copy.

This is what shallow copy is. __________________________________________________________________

  • Deep copy:

Changes in the mutable object or we can say in the sublist are reflecting in shallow copy, but I want that changes in the sublist should not affect my new copy.

For this purpose, we use the deep copy.

Before creating a deep copy we have to import the copy module.

Now let’s remove a value from the sublist of A and check whether it is reflecting in D or not.

Yes, now it is not affecting our new copy of A.

but the question is how?

In a deep copy, it creates a new physical copy of A as well as a new physical copy of the sublist in A.

Remember in shallow copy it refers to the sublist only not creates any new physical copy of a sublist.

This is how it works.

Thanks for reading !!

--

--