Iterating over several lists at once

Paul Rubin http
Wed Dec 13 09:36:57 EST 2006


"Gal Diskin" <gal.diskin at gmail.com> writes:
> I am writing a code that needs to iterate over 3 lists at the same
> time, i.e something like this:
> 
> for x1 in l1:
>     for x2 in l2:
>         for x3 in l3:
>             print "do something with", x1, x2, x3


This does look a little kludgy (untested):

    for x1,x2,x3 in ((x1,x2,x3) for x1 in l1 for x2 in l2 for x3 in l3):
       print "do something with", x1, x2, x3



More information about the Python-list mailing list