Cross product of nested lists

Hi

I’m truing to create a cross product on a nested list. I thought I could just set the list @L2 with lacing to cross product but it isn’t working.

The output should be:

0,3
0,4
0,5
0,6
0,7
1,3
1,4

20,13
20,14

What am I doing wrong?

Nested cross product.dyn (11.6 KB)

like this ?

sure someone else will do it more elegantly

No, that won’t work as the length of the list of lists will vary. It won’t always be two.

Solution using python

from itertools import product

OUT = zip(*[j for i in zip(IN[0], IN[1]) for j in product(*i)])

That loses the data structure, but can be fixed like this. Not sure if there is a more elegant solution via Python.

So, is a cross product of lists of lists not possible with OOTB nodes?

Nested cross product.dyn (26.8 KB)

The python code was flattening the points to keep things simple, you can keep list structure by only providing the product result. Note list levels on the nodes.

from itertools import product

OUT = (product(*i) for i in zip(IN[0], IN[1]))

Edit: Or use this in the original node

from itertools import product

OUT = zip(*(zip(*j) for j in (product(*i) for i in zip(IN[0], IN[1]))))
1 Like

1 Like

This is great!