On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote:
John J. Lee wrote:
Steve Holden <[EMAIL PROTECTED]> 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
I don't know that that approach is less cryptic. ips is already a list... it looks cryptic to make it a list again, doesn't it? IMO, the two are equally cryptic. The epitome of clarity would be copy(ips)... now *that* makes sense, of course, ips[:] or list(ips) work equally well to the programmer who has learned them.
Howsabout:
from copy import copy ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98',
... '127.0.0.1', '255.0.0.0', '255', '128.173.255.34']
for ip in copy(ips):
... if '255' in ip: ... ips.remove(ip) ...
ips
['128.173.120.79', '198.82.247.98', '127.0.0.1']
But I still think that the list comprehension is the best.
Peace Bill Mill bill.mill at gmail.com
Wow, I did not know that a copy module existed. I made all that up about copy being the perfect example here. Great minds think alike ;) I fell Guidoish.
--
http://mail.python.org/mailman/listinfo/python-list