Bug?

Alex Martelli aleaxit at yahoo.com
Wed Aug 1 07:17:12 EDT 2001


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:0hL97.8461$9i1.776789 at e420r-atl1.usenetserver.com...
> As a general rule it's a very bad idea to change a list you're iterating
> over with for.
>
> a-list-by-any-other-name-can-still-be-changed-ly y'rs  - steve
    ...
> > mainlist = [1, 2, 3, 4, 5]
> > copy = mainlist
> >
> > for item in copy:
> >  print item,
> >  mainlist.remove(item)

...and therefore the original poster should change his or her
code to ensure a copy of mainlist IS made.  e.g., changing
the second assignment statement to:

    copy = mainlist[:]

would suffice, because the whole-list slice [:] DOES perform
a (shallow, but that's sufficient here) copy of the list.


Alex






More information about the Python-list mailing list