del does not delete objects, removes links .
There is an object that is an integer value of 2 . This one object was mentioned in two places; a[1] and b .
You deleted a[1] so that the link doesnβt disappear. But this does not affect object 2 , only the link that was in a[1] . Thus, the link accessible via name b still reaches object 2 simply.
Even if you del all links that do not affect the object. Python is a garbage collection, so it is responsible for notifying when an object is no longer referenced anywhere, so that it can return the memory occupied by the object. This will happen some time after the object is no longer available. 1
1 CPython uses reference counting to implement garbage collection 2, which allows us to say that objects will usually be fixed as soon as their last link dies, but that the implementation detail is not part of the language specification. You do not need to understand exactly how Python collects its garbage and does not have to write programs that depend on it; other Python implementations, such as Jython, PyPy, and IronPython, do not implement garbage collection this way.
2 Plus an additional garbage collection mechanism for detecting circular garbage that cannot be dealt with by reference counting.
source share