Re: Removal of element from list while traversing causes the next element to be skipped

2008-02-01 Thread William McBrine
On Wed, 30 Jan 2008 06:07:45 -0800, cokofreedom wrote: > Anyone else noticed that the OP has not actually replied to any of the > suggestions... Sorry. I was just fascinated at the turns it was taking. But the first answer was fine for me: for name in apps[:]: etc. Thanks all. -- 09 F9 11

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-31 Thread Gabriel Genellina
En Thu, 31 Jan 2008 15:45:42 -0200, <[EMAIL PROTECTED]> escribió: > Hmm, how does this fare?? > > for i in range(len(a)): > if a[i]==99: a=a[:i]+a[i+1:] > > > I like following your guys code noodling. I can come up with something > that does what it appears your doing, sometimes, but as t

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-31 Thread Matthew_WARREN
Hmm, how does this fare?? for i in range(len(a)): if a[i]==99: a=a[:i]+a[i+1:] I like following your guys code noodling. I can come up with something that does what it appears your doing, sometimes, but as to it's relevant merits I havent a clue :) matt.

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread cokofreedom
Anyone else noticed that the OP has not actually replied to any of the suggestions... -- http://mail.python.org/mailman/listinfo/python-list

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Hrvoje Niksic
Paul Rubin writes: > Quadratic time!! Yowch!! Back to the future: > > def rocket_science(xs): >for x in xs: > if x != 99: > yield x > > a[:] = list(rocket_science(a)) I call "useless use of list"! a[:] = rocket_science(a) :-) -- http://mail.python

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Neil Cerutti
On 30 Jan 2008 05:20:49 -0800, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote: > "Neil Cerutti" <[EMAIL PROTECTED]> writes: > > Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet: > > > > i = 0 > > while i < len(a): > > if a[i] == 99: > > del a[i] > > else: > >

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Arnaud Delobelle
On Jan 30, 11:57 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > n = len(a) > for i, x in enumerate(a): >     if x == 99: del a[i-n] Oops. That can't work. Don't know what I was thinking here. I probably did had one mental refactoring too many... -- Arnaud -- http://mail.python.org/mailman/

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Paul Rubin
"Neil Cerutti" <[EMAIL PROTECTED]> writes: > Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet: > > i = 0 > while i < len(a): > if a[i] == 99: > del a[i] > else: > i += 1 Quadratic time!! Yowch!! Back to the future: def rocket_science(xs): for x in xs:

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread bearophileHUGS
If you don't want to reinvent the wheel all the time you can use this one: def inplacefilter(pred, alist): """inplacefilter(pred, alist): filters the given list like filter(), but works inplace, minimizing the used memory. It returns None. >>> pr = lambda x: x > 2 >>> l = [] >

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Neil Cerutti
On Jan 30, 2008 6:57 AM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > Or one could use the trick of counting from the right (untested): > > n = len(a) > for i, x in enumerate(a): > if x == 99: del a[i-n] Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet: i = 0 while i

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Arnaud Delobelle
On Jan 29, 10:59 pm, Paul Hankin <[EMAIL PROTECTED]> wrote: > If I really had to modify it in place (and the condition wasn't really > x == 99), how about: > bad_indices = [i for i, x in enumerate(a) if x == 99] > for bad_index in reversed(bad_indices): >     del a[bad_index] Or one could use the

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Paul Rubin
Santiago Romero <[EMAIL PROTECTED]> writes: > In a = [1, 2, 3, 3, 3, 4, 3, 3, 2, 3], the filter solution will > efectively remove all items with value == 3 while li.remove(3) will > only remove the first ocurrence. Hmm, interesting, I didn't realize that (shoulda checked the docs). Thanks! --

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread cokofreedom
On Jan 30, 9:50 am, Santiago Romero <[EMAIL PROTECTED]> wrote: > On 30 ene, 08:09, Paul Rubin wrote: > > > Santiago Romero <[EMAIL PROTECTED]> writes: > > > > > >>> li = [1,2,3,4,5] > > > > >>> filter(lambda x: x != 3, li) > > > > [1, 2, 4, 5] > > > > I haven't measure

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Santiago Romero
On 30 ene, 08:09, Paul Rubin wrote: > Santiago Romero <[EMAIL PROTECTED]> writes: > > > > >>> li = [1,2,3,4,5] > > > >>> filter(lambda x: x != 3, li) > > > [1, 2, 4, 5] > > > I haven't measured it, but this should be the fast solution in all > > the thread ... > > li.re

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Paul Rubin
Santiago Romero <[EMAIL PROTECTED]> writes: > > >>> li = [1,2,3,4,5] > > >>> filter(lambda x: x != 3, li) > > [1, 2, 4, 5] > > I haven't measured it, but this should be the fast solution in all > the thread ... li.remove(3) is probably faster. -- http://mail.python.org/mailman/listinfo/python-

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Santiago Romero
> how about > > >>> li = [1,2,3,4,5] > >>> filter(lambda x: x != 3, li) > [1, 2, 4, 5] I haven't measured it, but this should be the fast solution in all the thread ... -- http://mail.python.org/mailman/listinfo/python-list

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Joe Riopel
On Jan 29, 2008 9:23 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > If you're going to delete elements from > a list while iterating over it, then do > it in reverse order: how about >>> li = [1,2,3,4,5] >>> filter(lambda x: x != 3, li) [1, 2, 4, 5] >>> -- http://mail.python.org/mailman/listi

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Paul Hankin
On Jan 29, 8:17 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > Berteun Damman <[EMAIL PROTECTED]> wrote: > > On Tue, 29 Jan 2008 09:23:16 -0800 (PST), [EMAIL PROTECTED] > ><[EMAIL PROTECTED]> wrote: > >> If you're going to delete elements from > >> a list while iterating over it, then do > >> it in

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Paul Hankin
On Jan 29, 4:34 pm, William McBrine <[EMAIL PROTECTED]> wrote: > Look at this -- from Python 2.5.1: > > >>> a = [1, 2, 3, 4, 5] > >>> for x in a: > > ...     if x == 3: > ...         a.remove(x) > ...     print x > ... > 1 > 2 > 3 > 5 > > >>> a > [1, 2, 4, 5] > > Sure, the resulting list is correct

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Duncan Booth
Berteun Damman <[EMAIL PROTECTED]> wrote: > On Tue, 29 Jan 2008 09:23:16 -0800 (PST), [EMAIL PROTECTED] ><[EMAIL PROTECTED]> wrote: >> If you're going to delete elements from >> a list while iterating over it, then do >> it in reverse order: > > Why so hard? Reversing it that way creates a copy,

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Santiago Romero
> Look at this -- from Python 2.5.1: > > >>> a = [1, 2, 3, 4, 5] > >>> for x in a: > ... if x == 3: > ... a.remove(x) > ... print x Well ... you could use: >>> for i in range(len(a)-1, -1, -1): ...print a[i] ...if a[i] == 3: del a[i] ... 5 4 3 2 1 >>> print a [1, 2, 4, 5]

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Berteun Damman
On Tue, 29 Jan 2008 09:23:16 -0800 (PST), [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > If you're going to delete elements from > a list while iterating over it, then do > it in reverse order: Why so hard? Reversing it that way creates a copy, so you might as well do: >>> a = [ 98, 99, 100 ] >>>

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread [EMAIL PROTECTED]
On Jan 29, 8:34 am, William McBrine <[EMAIL PROTECTED]> wrote: > Look at this -- from Python 2.5.1: > > >>> a = [1, 2, 3, 4, 5] > >>> for x in a: > > ... if x == 3: > ... a.remove(x) > ... print x > ... > 1 > 2 > 3 > 5 > > >>> a > [1, 2, 4, 5] > > Sure, the resulting list is correct

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread imageguy
On Jan 29, 12:34 pm, William McBrine <[EMAIL PROTECTED]> wrote: > Look at this -- from Python 2.5.1: > > >>> a = [1, 2, 3, 4, 5] > >>> for x in a: > > ...     if x == 3: > ...         a.remove(x) > ...     print x > ... > 1 > 2 > 3 > 5 > > >>> a > [1, 2, 4, 5] > > Sure, the resulting list is correc

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Berteun Damman
On Tue, 29 Jan 2008 16:34:17 GMT, William McBrine <[EMAIL PROTECTED]> wrote: > Look at this -- from Python 2.5.1: > a = [1, 2, 3, 4, 5] for x in a: > ... if x == 3: > ... a.remove(x) > ... print x > ... > 1 > 2 > 3 > 5 a > [1, 2, 4, 5] You have to iterate over a cop

Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread William McBrine
Look at this -- from Python 2.5.1: >>> a = [1, 2, 3, 4, 5] >>> for x in a: ... if x == 3: ... a.remove(x) ... print x ... 1 2 3 5 >>> a [1, 2, 4, 5] >>> Sure, the resulting list is correct. But 4 is never printed during the loop! What I was really trying to do was this: apps =