string issue

Alex Martelli aleaxit at yahoo.com
Sun Feb 6 18:16:07 EST 2005


Bill Mill <bill.mill at gmail.com> wrote:
   ...
> > > You are modifying the list as you iterate over it. Instead, iterate over
> > > a copy by using:
> > >
> > > for ip in ips[:]:
   ...
> Once you know it, it's neat, and I use it sometimes. However, it's a
> little too "magical" for my tastes; I'd rather be more explicit about
> what's going on.

Using ips[:] to make a copy on the fly is very idiomatic, but I've never
liked it, personally.  I see no reason to prefer it to the even shorter
and arguably less obscure ips*1, for example.  My preference is:

  for ip in list(ips):


Alex



More information about the Python-list mailing list