Yes, all Python objects live on the heap (at least on CPython.) They are counted by reference: they are de-allocated when the last reference to the object disappears. (CPython also has a garbage collector to interrupt loops.)
In CPython, your first list disappears as soon as the function returns, since you did not bind the return value to the name, and the reference count fell to zero. In another implementation, an object can live longer until the garbage collector starts.
Some objects (for example, open files) have attached resources that are automatically released when the object is released, but because of the above, it is not recommended to rely on this. Resources should be closed explicitly when you are done with them.
Simon sapin
source share