Re: Removing from a List in Place

2006-09-06 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Tim Williams: > >>You could also use a list comprehension for your case >> >alist = [1 ,2 ,3] >alist = [x for x in alist if x != 2] >alist >> >>[1, 3] > > > The list comprehension filtering is the simpler and often the best > solution. For memory-conscious

Re: Removing from a List in Place

2006-09-05 Thread bearophileHUGS
Tim Williams: > You could also use a list comprehension for your case > >>> alist = [1 ,2 ,3] > >>> alist = [x for x in alist if x != 2] > >>> alist > [1, 3] The list comprehension filtering is the simpler and often the best solution. For memory-conscious people this is another possible (un-python

Re: Removing from a List in Place

2006-09-05 Thread John Machin
bayerj wrote: > > I'm going to assume that it's supposed to work like this, but could > > someone tell me the reasoning behind it? I.E. why is 3 skipped? > > Because: > > >>> alist[2] > 3 > > You are removing the third item, not the second. This is incorrect. You may need to remind yourself that

Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 5 Sep 2006 16:05:36 -0700, bayerj <[EMAIL PROTECTED]> wrote: > > I'm going to assume that it's supposed to work like this, but could > > someone tell me the reasoning behind it? I.E. why is 3 skipped? > > Because: > > >>> alist[2] > 3 > > You are removing the third item, not the second. > Actu

Re: Removing from a List in Place

2006-09-05 Thread bayerj
> I'm going to assume that it's supposed to work like this, but could > someone tell me the reasoning behind it? I.E. why is 3 skipped? Because: >>> alist[2] 3 You are removing the third item, not the second. -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote: > On 9/5/06, Tim Williams <[EMAIL PROTECTED]> wrote: > > > It does already, you just haven't grasped list fully yet :):) > > > > > > when you remove 2 from alist, the list becomes length 2, there is no > > > longer a 3rd item in the list to i

Re: Removing from a List in Place

2006-09-05 Thread Gregory Piñero
On 9/5/06, Tim Williams <[EMAIL PROTECTED]> wrote: > > It does already, you just haven't grasped list fully yet :):) > > > > when you remove 2 from alist, the list becomes length 2, there is no > > longer a 3rd item in the list to iterate over. > > > > Try this > > > > > >>> alist=[1 ,2 ,3, 4] >

Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Tim Williams <[EMAIL PROTECTED]> wrote: > On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:> I'm going > to assume that it's supposed to work like this, but could > > someone tell me the reasoning behind it? I.E. why is 3 skipped? > > > > >>> alist=[1,2,3] > > >>> for item in ali

Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:> I'm going to assume that it's supposed to work like this, but could > someone tell me the reasoning behind it? I.E. why is 3 skipped? > > >>> alist=[1,2,3] > >>> for item in alist: > print item > if item==2: >

Removing from a List in Place

2006-09-05 Thread Gregory Piñero
I'm going to assume that it's supposed to work like this, but could someone tell me the reasoning behind it? I.E. why is 3 skipped? >>> alist=[1,2,3] >>> for item in alist: ... print item ... if item==2: ... alist.remove(item) ... 1 2 >>> Bonus Question: Can we m