Re: [pygame] Iterate over list and delete

2018-05-20 Thread Ian Mallett
On Sun, May 20, 2018 at 11:20 PM, Daniel Foerster wrote: > I would guess that a significant amount of the gain is that he doesn't > have to len() the list every iteration, plus the item unpacking occurs in C. > ​`len(...)` should be constant-time (stored with array), but indeed caching it in a va

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
I would guess that a significant amount of the gain is that he doesn't have to len() the list every iteration, plus the item unpacking occurs in C. I don't know how much JIT would affect anything unless you ran the tests in PyPy. On Sun, May 20, 2018, 23:51 Ian Mallett wrote: > On Sun, May 20, 2

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Ian Mallett
On Sun, May 20, 2018 at 9:25 PM, Daniel Foerster wrote: > The relevance of N exponent versus the discarded coefficients depends on > how big N may be. With the likely sizes of N in a Pygame class, the > difference between the algorithms seems probably negligible. A quick test > with 20% deletion

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
This is a quality algo improvement. I will note that the del can just be "del myList[i_write:]" without the manually calculated upper bound. On Sun, May 20, 2018, 23:39 MrGumm wrote: > I like this one. I would modify somewhat. If you don't like "not" you > could change the function name to keep_

Re: [pygame] Iterate over list and delete

2018-05-20 Thread MrGumm
I like this one. I would modify somewhat. If you don't like "not" you could change the function name to keep_item() and reverse the logic. i_write =0 for itemin myList: if not needsToBeDeleted(item): myList[i_write] = item i_write +=1 del myList[i_write:len(myList)] On 5/20

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
Tyler: That's clever, but still runs into the nasty nasty problems of using .remove(), which requires iteration over the entire list. In practice, it's 3x slower than either index-tracking algorithm at N=1000, while being slightly faster at N=100 and 50% faster at N=10— but almost 20x slower at N=

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Tyler Laing
What about reversing through the list? for val in reversed(myList): if check(val): myList.remove(val) The way it works is that reversed returns an iterator that doesn't copy from your list, but handles changes in the list correctly for you. So its both memory and time efficient (one p

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
The relevance of N exponent versus the discarded coefficients depends on how big N may be. With the likely sizes of N in a Pygame class, the difference between the algorithms seems probably negligible. A quick test with 20% deletion shows that your algorithm becomes more efficient around N=7000, bu

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Ian Mallett
On Sun, May 20, 2018 at 8:35 PM, Daniel Foerster wrote: > The third, and probably most convenient based on where you seem to be at > in the curriculum, is to do something like Ian suggested. I think there's a > simpler way to do it with similar performance though I've not benched to > find out; I

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
A couple of options still then. The simplest on paper is #3 of your original options, but as you mentioned, list.remove() has problems (and bigger problems if you are working with potentially duplicate items). A second option is stay as close to the list comprehension as possible, using either th

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Ian Mallett
On Sun, May 20, 2018 at 5:55 PM, Irv Kalb wrote: > Is there a way to do that without the list comprehension? I'm building > this as part of a class, and I will not have talked about list > comprehensions up until that point. > ​A list comprehension is the clearest and most-pythonic way to do thi

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Irv Kalb
Thanks very much. Is there a way to do that without the list comprehension? I'm building this as part of a class, and I will not have talked about list comprehensions up until that point. Thanks, Irv > On May 20, 2018, at 2:35 PM, Daniel Foerster wrote: > > I think what you're looking for

Re: [pygame] Iterate over list and delete

2018-05-20 Thread Daniel Foerster
I think what you're looking for is this: my_list = [x for x in my_list if not need_to_delete(x)] On Sun, May 20, 2018, 16:28 Irv Kalb wrote: > I am building a game where I am keeping track of many objects in a list > (let's just call it "myList". In every "frame" of my game I need to see if >

[pygame] Iterate over list and delete

2018-05-20 Thread Irv Kalb
I am building a game where I am keeping track of many objects in a list (let's just call it "myList". In every "frame" of my game I need to see if any objects need to be removed from myList. I know that this code: for item in myList: if needsToBeDeleted(item): myList.remove(item)