lnNoam - 11 months ago 49

Python Question

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

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)
```

Source (Stackoverflow)