Initiating Multiple Python Lists

Is there a smart 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:

 for a in list1: for b in list2: foo(a, b) 
+6
source share
1 answer

itertools.product() does just that:

 for a, b in itertools.product(list1, list2): foo(a, b) 

It can handle an arbitrary number of iterations, and in this sense is more general than a nested for loop.

+13
source

Source: https://habr.com/ru/post/914739/


All Articles