lnNoam lnNoam - 5 months ago 14
Python Question

Replacing linked (recursive) for loops with a more functional approach

If I have:

for a in range(100):
for b in range(50):
my_func(a, b)

I can replace that code with:

from itertools import product
product(*[range(100), range(50)])

But say I have the following:

for i in range(100):
for j in range(i):
my_func(i, j)

How can I perform a similar replacement (assuming this is possible)?


Not sure how to do it with itertools but wouldn't a list comprehension or generator expression suffice?

gen = ((i, j) for i in range(100) for j in range(i))

for i, j in gen:
    my_func(i, j)