how to clear up a List in python?

Duncan Smith buzzard at urubu.freeserve.co.uk
Fri May 26 13:23:31 EDT 2006


chris brat wrote:
> Doesnt this do what the original poster is try accomplish?
> 

Not what the OP asked for, no.  Clearing a list implies that list1
should still be bound to the same list (which might be important if
there are other names bound to the same list).  If it wasn't important
that it be bound to the same list then I would probably go with
linnorm's approach (which isn't really 'clearing' the list).

>>> list1 = [0,1,2,3]
>>> id(list1)
10772728
>>> del list1[:]
>>> list1
[]
>>> id(list1)
10772728  # i.e. the same list
>>> list1 = [0,1,2,3]
>>> id(list1)
10675264
>>> list1 = []
>>> id(list1)
10603576  # i.e. a different list
>>>

Duncan



More information about the Python-list mailing list