A permutation on permutations

Kragen Sitaker kragen at canonical.org
Sun Nov 25 21:37:05 EST 2001


"Arthur Siegel" <ajs at ix.netcom.com> writes:
> The following little func does not work as I 
> would expect!!!
> 
> Something about the iteration of p in t 
> with the list.remove().
> 
> Is the func as written certifiably bad 
> Python ,  or a bug/trap I fell into?
> 
> def removedups(t):
>    for p in t:
>       if p[-1]>p[0]:
>          t.remove(p)
>    print t   

It's certifiably bad Python, because modifying a list you're iterating
over is a bug/trap you fell into.  One general way of solving this is
to say for p in t[:]: instead of for p in t[:], but I'd probably say
print [p for p in t if p[-1] > p[0]] instead of the whole function.




More information about the Python-list mailing list