473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2492
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 would not remove. Any help would be greatly...
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 sort of trivial task but i found no other way than...
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 Josuttis). It also says, that remove returns a...
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 (that I could understand) on this site, and it works....
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, I want to remove any elements in B, that are also...
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 the code: --------- #include<string> class...
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 what I can do. ...
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 UIandDB {
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 generic list of my first class. This custom listneeds...
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 &); void pop_back();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.