473,326 Members | 2,133 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,326 software developers and data experts.

iterating over collection, deleting entries

I want to iterate over a collection and delete all unwanted entries.

for item in collection:
del item

of course doesn´t do anything useful. Two things come to mind:
a) iterating backwards over the collection using indexing, something like:

for i in range(len(collection)-1, -1, -1):
item = collection[i]
if isBad(item) del collection[i]

b) duplicating the collection, iterating over one and deleting from the
other.

Both solutions seem both ugly and expensive to me, solution a)
probably isn´t even O(x) anymore.

Please tell me, what´s the best way to do this?
--
Patrick von Harsdorf
pa*****@harsdorf.de


Jul 18 '05 #1
3 1796
Sorry, I missed that SnuSnu just posed a very similar question.
Please ignore my post...
Jul 18 '05 #2
On Sun, 25 Apr 2004 18:25:08 +0200, "Patrick von Harsdorf"
<pa*****@harsdorf.de> wrote:
Please tell me, what´s the best way to do this?


I'm pretty new to python, but normally this is done
by a simple read-write approach. You iterate over
the elements using a "read" pointer, and every time
you find one you want to keep you just write it
and increment the write pointer.
In code...

wrp = 0

for x in container:
if is_good(x):
container[wrp] = x
wrp += 1

del container[wrp:]

This is O(n) and doesn't require any additional memory

I suppose however that it's not so common finding
cases in which this is much better than...

a = [x for x in a if is_good(x)]

The latter will allocate a list for the result
(instead of reusing the same), but can free the
original (and this may be *better* than just
resizing it - it may be better in C++, for example).

HTH
Andrea
Jul 18 '05 #3
I would do:

collection=[i for i in collection if not isBad(i)]

Larry Bates
Syscon, Inc.
"Patrick von Harsdorf" <pa*****@harsdorf.de> wrote in message
news:c6*************@news.t-online.com...
I want to iterate over a collection and delete all unwanted entries.

for item in collection:
del item

of course doesn´t do anything useful. Two things come to mind:
a) iterating backwards over the collection using indexing, something like:

for i in range(len(collection)-1, -1, -1):
item = collection[i]
if isBad(item) del collection[i]

b) duplicating the collection, iterating over one and deleting from the
other.

Both solutions seem both ugly and expensive to me, solution a)
probably isn´t even O(x) anymore.

Please tell me, what´s the best way to do this?
--
Patrick von Harsdorf
pa*****@harsdorf.de

Jul 18 '05 #4

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

Similar topics

4
by: Saso Zagoranski | last post by:
Hi! I need an array-type of collection when the entries would look like: object myObject, int myInt, string myString Which collection should I use? I know I could declare a multidimensional...
1
by: Josema | last post by:
Hi, My problem is the next one (in a windows application): - I have a class derived from collectionbase to fill with persons object (id, name) from database. - I have a ListBox wich datasource...
8
by: orekin | last post by:
Hi There I have a huge array of objects being displayed on a UI. These are actually 'jobs' that the users need to deal with before close of business. As the operator deals with a particular...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
5
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
3
by: Bob Altman | last post by:
Hi all, I read somewhere that the STL map class stores entries internally sorted by the key. I wrote a small test program (below) that verifies that iterating through the map returns entries...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.