string issue

John J. Lee jjl at pobox.com
Fri Feb 4 15:13:44 EST 2005


Steve Holden <steve at holdenweb.com> writes:
[...]
> You are modifying the list as you iterate over it. Instead, iterate
> over a copy by using:
> 
> for ip in ips[:]:
>    ...

Just to help popularise the alternative idiom, which IMO is
significantly less cryptic (sane constructors of mutable objects
almost always make a copy, and list is no exception: it's guaranteed
to do so):

for ip in list(ips):
   ...


Works back to at least Python 1.5.2.


John



More information about the Python-list mailing list