Removing from a List in Place

Tim Williams tim at tdw.net
Tue Sep 5 19:29:26 EDT 2006


On 5 Sep 2006 16:05:36 -0700, bayerj <bayerj at in.tum.de> 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.
>

Actually,  he's removing 2 from the list,   but then the length of the
list shrinks by 1 and iteration stops.

The example would have been better if alist = ['a','b','c'] and 'b' was removed.

 L.remove(value) -- remove first occurrence of value

you were possibly  thinking of alist.pop(2),  which removes the item
alist[2] from alist

HTH :)


-- 

Tim Williams



More information about the Python-list mailing list