[Tutor] question about removing items from a list

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 15 Aug 2001 10:21:00 -0700 (PDT)


On Wed, 15 Aug 2001, 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?

Garbage collection: Python won't really delete things until they're
inaccessable to us.

>From one point of view, the really big difference between:

    del lista

and

    lista = []

is that 'del' will also remove the name from our namespace:


###
>>> lista = ['forth', 'eorlingas!']
>>> lista = []
>>> lista
[]
>>> lista = ['there', 'is', 'no', 'spoon']
>>> del lista
>>> lista
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'lista' is not defined
###

So using 'del' depends on our expectation to use the name 'lista' later in
the program.