472,342 Members | 1,747 Online

# a bug in list.remove?

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?
Cheers
Astan

Aug 18 '06 #1
2 2446
Astan Chee:

(This is a small trap of Python, that it shares with some other
languages, and it shows that it may exist a language with a higher
level than Python.)
Generally in Python you can't modify a sequence that you are iterating
on.
There are some ways to avoid the problem. You can create a duplicate of
the ps list:

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)
print ps

Or:

for p in list(ps):
if p in qs:
ps.remove(p)
print ps

Or:

import copy
for p in copy.copy(ps):
if p in qs:
ps.remove(p)
print ps

Or you can adopt a different strategy:

print [el for el in ps if p not in qs]

This algorithm is O(n*m), so if the two lists are long, you may need
too much time to run that. To speed up the program you can do this
(Python 2.4):

sqs = set(qs)
print [el for el in ps if p not in sqs]

Bye,
bearophile

Aug 18 '06 #2
Astan Chee <st***@al.com.auwrites on Sat, 19 Aug 2006 03:34:26 +1000:
>>for p in ps:
if p in qs:
ps.remove(p)
You are modifying an object ("ps") while you iterate over it.
This is a receipe for surprises...

The standard idiom is to iterate over a copy rather than the object itself:

for p in ps[:]:
....
Dieter
Aug 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.