2 ms·
The relevant function is itertools.product() [1]. It can give the Cartesian product of arbitrary iterables, but if you have the same iterable more than once, as
by quietbritishjim 8y ago
The relevant function is itertools.product() [1]. It can give the Cartesian product of arbitrary iterables, but if you have the same iterable more than once, as in this example, you can take advantage of the repeat= optional parameter to simplify the call a little bit:
for x, y in itertools.product(range(10), repeat=2):
if condition(x, y):
dosomething(x, y)
break
Alternatively, you can use Python list comprehensions:
prodlist = [x, y for x in range(10) for y in range(10)]
for x, y in prodlist:
if condition(x, y):
dosomething(x, y)
break
Overall, I still prefer refactoring into a function and using return, as the GP comment suggested.
[1] https://docs.python.org/3/library/itertools.html#itertools.product https://docs.python.org/3/library/itertools.html#itertools.p...