deleting elements from a list in a for loop

flupke flupke at nonexistingdomain.com
Fri Oct 29 10:48:26 EDT 2004


Pierre Barbier de Reuille wrote:

> Hi !
> 
> Indeed, you cannot erase elements in a list you're iterating on !
> a possibility could be :
> 
> to_delete = []
> for i in xrange(len(el)):
>   if condition:
>     to_delete.insert(0, i)
> for i in to_delete:
>   del el[i]
> 
> That should work fine ...
> 
> flupke a écrit :

Thanks,

that works great!

Benedict



More information about the Python-list mailing list