Python 101 : Deep copies and shallow copies - List example -


In the below example, we make a shallow copy "l2" of a list "l1" using regular "=" assignment, we also make a deep copy "l3" of "l1".

We remove one elements from "l1" and see the impact on both "l2" and "l3".


Looking at the different addresses of the lists using the "id" function , we see that "l1" and "l2" share the same object.

"l3" is a deep copy of "l1", so its elements refers to another list. 

Comments

Leave as a comment:

Archive