REALLY need help with iterating a list.

Fredrik Lundh fredrik at
Mon Jun 11 21:23:28 CEST 2007

infidel wrote:

> How about something like this?
> while serverlist:
>     server = serverlist.pop(0)
>     pinger = ping[server]
>     if pinger.returncode==None:
>         pinger.poll()
>         serverlist.append(server)
>     else:
>         pingresult[server] =
>         pingreturncode[server] = pinger.returncode
> Basic idea:  as long as there are servers in the list, pop the first
> one out of the list, see if it's done, and if it isn't, put it back on
> the end of the list.

here's a simple variation of that, which is a bit more efficient, and 
perhaps also a bit easier to use in the general case:

while serverlist:
     still_active = []
     for server in serverlist:
	pinger = ping[server]
	if pinger.returncode is None:
	    pingresult[server] =
	    pingreturncode[server] = pinger.returncode
     serverlist = still_active


More information about the Python-list mailing list