Q:

Using numpy arrays to avoid for loops - combinatorics

There's got to be a more pythonic way of doing:
r = np.arange(100)
results = []

for i in r:
    for j in r:
        for k in r:
            for l in r:

                #Here f is some predefined function
                if f(i,j,k,l) < 5.0:
                     results.append(f(i,j,k,l))

I'm sure using arrays can simplify this somehow, I'm just not sure how. Thanks!

A:

Use itertools cartesian product:
import itertools
r = np.arange(100)
results = []
for (i,j,k,l) in itertools.product(r,repeat=4):
    if f(i,j,k,l) < 5.0:
         results.append(f(i,j,k,l))

Or even more compact way, using list comprehension:
[ f(i,j,k,l) for (i,j,k,l) in itertools.product(r,repeat=4) if f(i,j,k,l) < 5.0 ]

