469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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?
Thanks for your help!
Cheers
Astan

Aug 18 '06 #1
2 2204
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Stan Cook | last post: by
6 posts views Thread by Ishwor | last post: by
6 posts views Thread by Arne Claus | last post: by
7 posts views Thread by Adam Hartshorne | last post: by
1 post views Thread by SKP | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by
12 posts views Thread by isliguezze | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.