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 iterate over. > > > > > > Try this > > > > > > > >>> alist=[1 ,2 ,3, 4] > > > > >>> for item in alist: > > > > .... print item > > > > .... if item==2: > > > > .... alist.remove(item) > > > > .... print alist > > > > > > > sorry, I meant to offer an alternative also > > > > in yourgiven case you can iterate over a copy of the list like this: > > > > >>> for item in alist[:] : > > > > Thanks Tim. I suppose that does make sense. Iterating over a copy is > a good alternative. >
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] >>> :) -- http://mail.python.org/mailman/listinfo/python-list