In Python, the = operator when used, doesn’t create a copy of the object.
Instead, it is just another reference or label to the same object.
To create copies, Python offers 2 different types of functions from its Copy module.
Shallow Copy creates a new object, but only copies the references to the original elements, and not the specific elements themselves.
From the code in the image, changing an element in my_list also reflects in the elements of shallow_list, as they both point to the same object.
However, if you append another element to my_list, it won’t reflect in shallow_list, as it doesn’t contain the reference to that new object.
Deep Copy, on the other hand, creates a new object and copies all nested the elements present within the original list recursively.
So, any changes made to my_list won’t affect deep_list, as they point to two separate objects.
#python #datascience #machinelearning