[Tutor] question about removing items from a list

Python Snake pythonpython@hotmail.com
Wed, 15 Aug 2001 14:13:45 +0900


Many thanks to Lloyd and Allan for explaining the tricks behind for loop.

Anyway, if you really want to loop through the list and remove all the items
one by one, you may try something like this:

>>> lista = ['a', 'b', 'c', 'd']
>>> templist = lista[:]
>>> for item in templist:
...        lista.remove(item)
...
>>> lista
[]


Regards,

HY


----- Original Message -----
From: "John Kenyon" <John.Kenyon@mail.idrive.com>
To: <tutor@python.org>
Sent: Wednesday, August 15, 2001 9:42 AM
Subject: [Tutor] question about removing items from a list


> Can anyone explain why the following works the way it does?
>
> >>>lista = ['a', 'b', 'c', 'd']
> >>>for item in lista:
> ... lista.remove(item)
> ...
> >>>lista
> ['b', 'd']
>
> thanks much,
>
> jck
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>