A way to simplify this problem is to simply take all the permutations of the second array and then perform an n-to-n mapping between the first array and each second permuted array.
For example, if you have [1,2] and [3,4], first compute the permutations [3,4] → {[3,4], [4,3]}, and then pair each to [1,2 ]. As a result, we obtain {[((1,3), (2,4)], [(1,4), (2,3)]}.
I have included an example implementation in Python below.
import itertools a = [1,2] b = [3,4] for p in itertools.permutations(b): print zip(a,p)
source share