a bug in list.remove?

Astan Chee stanc at al.com.au
Fri Aug 18 13:34:26 EDT 2006


Hi all,
I have 2 lists. What Im doing is check the first list and remove all 
occurances of the elements in the second list from the first list, like so:
 >>> ps = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
 >>> qs = [6,7,8,9,10,11,12,1,2]
 >>> for p in ps:
    if p in qs:
        ps.remove(p)

The problem Im having is that when I do
 >>> print ps
 it gives me
[2, 3, 4, 5, 7, 9, 11, 13, 14, 15]
which is incorrect since 2,7,9,11 shouldnt be in that list. Is this a 
bug in .remove? or is my algorithm somewhat flawed?
Thanks for your help!
Cheers
Astan




More information about the Python-list mailing list