[BangPypers] How for and list.remove() works

Venkatraman S venkat83 at gmail.com
Wed Jul 9 17:40:21 CEST 2008


On Wed, Jul 9, 2008 at 8:47 PM, Kushal Das <kushaldas at gmail.com> wrote:

> Hi all,
>
> >>> a = [12, 12, 1321, 34, 23, 12, 34, 45, 77]
> >>> for x in a:
> ...   if x == 12:
> ...     a.remove(x)
> >>> a
> [1321, 34, 23, 12, 34, 45, 77]
>
> Can any one explain me how the remove works and how it is effecting the for
> loop.
>

do not remove when using iterators.

prefer :
[x for x in a if x != 12]

Venkat
Blog @ http://blizzardzblogs.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20080709/d238a16e/attachment.htm>


More information about the BangPypers mailing list