# How can I generate a generalized product to two lists in a special way? [duplicate]

Let's say I have two lists, and I'd like to generate a "cartesian product" from these two. Specifically

Inputs:

A = {{{1}}, {{2}}, {{1}, {2}}};
B = {8, 9};


Desired output:

X = {{{{1}} -> 8, {{1}} -> 9}, {{{2}} -> 8, {{2}} -> 9}, {{{1}, {2}} ->
8, {{1}, {2}} -> 9}}


I tried

f[#1, #2]& /@ {A, B}


but it didn't work. I also tried to use Thread, but that didn't work either.

### Edit

Outer doesn't really work either, since it goes to all the leaves and extracts them first.

Outer[#1 -> #2, A, B]


{{{{1 -> 8, 1 -> 9}}}, {{{2 -> 8, 2 -> 9}}}, {{{1 -> 8, 1 -> 9}}, {{2 -> 8, 2 -> 9}}}}

-

## marked as duplicate by R. M.♦Jul 3 '13 at 19:48

Have you seen the 3rd argument of Outer? Outer[Rule, A, B, 1] – R. M. Jul 3 '13 at 17:46
@rm-rf I have now! – cartonn Jul 3 '13 at 17:47
and outer is much faster than my Table suggestion. – bobthechemist Jul 3 '13 at 18:00
People, surely this question is a duplicate. Please exert the effort to look for duplicates before answering; thanks. – Mr.Wizard Jul 3 '13 at 18:11
This is a duplicate of the linked post with the only difference being that Rule is the function instead of Join. There are several answers there, most/all of which should be applicable here (cc @Mr.Wizard) – R. M. Jul 3 '13 at 19:50

You just need to apply the third argument of Outer as rm -rf suggested. With your A and B,

Outer[#1 -> #2 &, A, B, 1]


gives

{{{{1}} -> 8, {{1}} -> 9}, {{{2}} -> 8, {{2}} -> 9}, {{{1}, {2}} ->  8, {{1}, {2}} -> 9}}


as you requested. To check (with your X)

Outer[#1 -> #2 &, A, B, 1] == X

True

-

I may be misinterpreting, but is this what you are looking for ?

Outer[f, {a, b, c}, {1, 2, 3}]

(* {{f[a, 1], f[a, 2], f[a, 3]}, {f[b, 1], f[b, 2],
f[b, 3]}, {f[c, 1], f[c, 2], f[c, 3]}} *)

-

A = {{{1}}, {{2}}, {{1}, {2}}};
B = {8, 9};
Table[{i -> j}, {i, A}, {j, B}]


which gives me:

{{{{{1}} -> 8}, {{{1}} -> 9}}, {{{{2}} -> 8}, {{{2}} ->
9}}, {{{{1}, {2}} -> 8}, {{{1}, {2}} -> 9}}}


I've got one too many sets of braces, it looks like.

-
Changing {i->j} to First@{i->j} gives the desired output I think. – bobthechemist Jul 3 '13 at 17:51