"Gal Diskin" wrote: > 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
how about for x1, x2, x3 in func(l1, l2, l3): print x1, x2, x3 where func is defined as, say, def func(l1, l2, l3): return ((x1, x2, x3) for x1 in l1 for x2 in l2 for x3 in l3) or if you prefer def helper(l1, l2, l3): for x1 in l1: for x2 in l2: for x3 in l3: yield x1, x2, x3 </F> -- http://mail.python.org/mailman/listinfo/python-list