473,396 Members | 1,775 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,396 software developers and data experts.

Efficient way to remove objects from a list

Hi all,

Today I wrote some code like this:

for m in self.messages:
if not m.finished:
continue

#process the message

fini = [m for m in self.messages if m.finished]
for m in fini:
self.messages.remove(m)

As you can, I want to find these finished messages in
"self.messages",
process them, and then remove them from the list.

Because a list can not be modified while iterating it, I have to use
a list "fini" to accomplish the target.

I found a smell of bad performance here.
Is there any faster ways?
Nov 3 '08 #1
2 1565
Chris Rebert wrote:
On Mon, Nov 3, 2008 at 1:40 AM, 一首诗 <ne******@gmail.comwrote:
>Hi all,

Today I wrote some code like this:

Build a new list as you go, then overwrite the old list with it.

unfinished = []
> for m in self.messages:
if not m.finished:
unfinished.append(m)
> continue

#process the message

Remove the following code
> fini = [m for m in self.messages if m.finished]
for m in fini:
self.messages.remove(m)

self.messages[:] = unfinished
Just

self.messages = unfinished

if also OK unless you have multiple references to the self.messages list.
This way you aren't calling .remove() multiple times and only iterate
through the list once.
Nov 3 '08 #2
Thanks! That's a more clear way!

On Nov 3, 9:38 pm, Peter Otten <__pete...@web.dewrote:
Chris Rebert wrote:
On Mon, Nov 3, 2008 at 1:40 AM, Ò»Ê×Ê« <newpt...@gmail.com>wrote:
Hi all,
Today I wrote some code like this:
Build a new list as you go, then overwrite the old list with it.
unfinished = []
for m in self.messages:
if not m.finished:
unfinished.append(m)
continue
#process the message
Remove the following code
fini = [m for m in self.messages if m.finished]
for m in fini:
self.messages.remove(m)
self.messages[:] = unfinished

Just

self.messages = unfinished

if also OK unless you have multiple references to the self.messages list.
This way you aren't calling .remove() multiple times and only iterate
through the list once.

Nov 3 '08 #3

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

Similar topics

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...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
22
by: Curious | last post by:
Hi, I am searching for a data structure that stores key-value pairs in it. This data structure is to hold large amounts of key-value pairs, and so needs to be efficient both in insertion and...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
16
by: Dustan | last post by:
I have a program that uses up a lot of CPU and want to make it is efficient as possible with what I have to work with it. So which of the following would be more efficient, knowing that l is a list...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
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. ...
5
by: bob | last post by:
Hi, My app needs to read a text file, compare the data to that already stored in the DB and generate a text file of the differences. The UI displays the text file data and the db data in a...
3
by: Rainer Queck | last post by:
Hello NG, is it possible to use linq to remove certain objects from a generic list? Something more or less like: from obj in MyList where obj.Id 25 delete obj
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.