Iterating through two lists

Oleg Broytmann phd at phd.pp.ru
Fri May 24 08:19:49 EDT 2002


On Fri, May 24, 2002 at 07:14:16AM -0500, Mark McEahern wrote:
> #! /usr/bin/env python
> 
> from __future__ import generators
> 
> def walklists(m, n):
>     i = 0
>     while 1:
>         if i >= len(m) or i >= len(n):
>             break
>         yield (m[i], n[i])
>         i += 1

   You can safeley return from a generator:

def walklists(m, n):
    i = 0
    while 1:
        if i >= len(m) or i >= len(n):
            return
        yield (m[i], n[i])
        i += 1

   IMO this makes the code a bit more clear.

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list