Is there a clever way to iterate over two lists in Python (without using list comprehension)?
I mean, something like this:
# (a, b) is the cartesian product between the two lists' elements
for a, b in list1, list2:
foo(a, b)
instead of:
for a in list1:
for b in list2:
foo(a, b)
itertools.product()does exactly this:It can handle an arbitrary number of iterables, and in that sense is more general than nested
forloops.