Iterating over several lists at once

John Henry john106henry at hotmail.com
Wed Dec 13 17:49:44 EST 2006


Carl Banks wrote:
<snip>
>
> The function can be extended to allow arbitrary arguments.  Here's a
> non-minmal recursive version.
>
> def cartesian_product(*args):
>     if len(args) > 1:
>         for item in args[0]:
>             for rest in cartesian_product(*args[1:]):
>                 yield (item,) + rest
>     elif len(args) == 1:
>         for item in args[0]:
>             yield (item,)
>     else:
>         yield ()
> 
> 

Very nice.




More information about the Python-list mailing list