[Tutor] question about removing items from a list

Allan Crooks allan.crooks@btinternet.com
Wed, 15 Aug 2001 15:52:38 +0100


On 15 Aug 2001, at 9:51, Lance E Sloan wrote:

> 
> "Allan Crooks" wrote:
> > An even better way would be like this:
> > 
> > del lista[:]
> > 
> > That would delete all items within the list. Doing 'del lista' would
> > simply delete the list itself.
> 
> What about:
> 
>   lista = []
> 
> That would give the intended effect, but I wonder what happens to the
> original list that lista pointed to.  Does it hang around in memory,
> or does Python's garbage collection get rid of it as soon as nothing
> points to it any longer?

The garbage collector deletes the old list right away.

Initially I did this (and tried variations, like lista = lista[0:0}) as an 
attempt to remove all elements in the list, and both lists had the 
same id() address, but I then realised it was Python removing the 
old list and putting the new one in the same memory slot

And then I remembered about the "del" command, and suggested 
that. :)

Allan.