472,342 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 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.

Similar topics

23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they...
6
by: Ishwor | last post by:
i am trying to remove an item 'e' from the list l but i keep getting IndexError. I know the size of the list l is changing in the for loop & its...
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by...
8
by: sara | last post by:
I am learning Access and programming. I wanted to have the user select the departments for an ad from the list of all departments. Found code...
7
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could tell me if there is an efficient way to do the following. Say I have a list(or vector) A and a list B,...
1
by: SKP | last post by:
Hi, I am trying do a basic liked list program, where i am adding nodes at the end. Adding part is fine, but removing part is not working. Here is...
4
by: =?Utf-8?B?emhhbmdscg==?= | last post by:
Hi, Following code does not work. (it will crash.)-- I think the reason is that I cannot modify list (call remove) in foreach. But I don't know...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.