On Dec 13, 3:47 pm, "Gal Diskin" <[EMAIL PROTECTED]> wrote: > Hi, > 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 > > What I need to do is go over all n-tuples where the first argument is > from the first list, the second from the second list, and so on... > > I was wondering if one could write this more easily in some manner > using only 1 for loop. > What I mean is something like this: > > for (x1,x2,x3) in (l1,l2,l3): > print "do something with", x1, x2, x3 > > Or maybe like this: > > for x1 in l1, x2 in l2, x3 in l3: > print "do something with", x1, x2, x3 > > However, this code obviously doesn't work... > > I'd be very happy to receive ideas about how to do this in one loop and > with minimal initialization (if at all required). > > Thanks in advance,Gal
Sorry for bumping this thread up. I just got back from a vacation and I had to say thanks to all the people that took the time to answer me. To Maksim - that's not what I meant, I want to iterate over the cartesian product of the lists (all ordered tuples, _not_ the tuple created from the first item in each list, then the tubple created from the second item in each list... as your example does). Thanks, Gal -- http://mail.python.org/mailman/listinfo/python-list