python garbage collector behavior on compound objects
Does python garbage collector cleans up a compound object if some of its parts are still referenced e.g. def foo(): A = [ [1, 3, 5, 7], [2, 4, 6, 8]] return A[1] B = foo() Will A[0] be garbage collected? Is there a way to confirm the same through code? Nothing references the list A and the nested list A[0] , so yes, they will be deleted from memory. The nested list object referenced by A[1] has no connection back to its original container. Note that it's not the garbage collector that does this; the GC only deals in breaking circular references. This simple case is handled entirely by