lnNoam lnNoam - 1 year ago 87
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)?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download