[Python-ideas] Function to unnest for-statements

Georg Brandl g.brandl at gmx.net
Fri May 23 11:47:32 CEST 2008


Carl Johnson schrieb:

> for a in [True, False]:
>     for b in [True, False]:
>         for c in [True, False]:
>             print a, b, c
> 
> Which yields
> 
> True True True
> True True False
> True False True
> True False False
> False True True
> False True False
> False False True
> False False False
> 
> But if you don't know how many variables you'll have, you're stuck  
> writing a complicated function instead of using a nice, simple for-loop.
> 
> So, going back to the first example, wouldn't it be nicer to write:
> 
> for x, y, z in combine(xs, ys, zs):
>     #Do something

> So, what do you think? Is this a common enough need that it should be  
> built into itertools?

Presumably, since it has been added to itertools in 2.6 under the name
product(). (Maybe Raymond borrowed the time machine?) :)

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list