# Thread map to 2 lists of inputs, including the nondiagonal terms

Given a function f and two lists of variables

{a1,a2,...}, {b1,b2,...}


How can I generate a following list

{f[a1,b1],f[a1,b2],...,f[a2,b1],f[a2,b2]...,}


==========================================

Thanks for the answering of @Bill first. I am not familiar with the function "Mapthread". The best thing I can do is as following

Mapthread[f,{l1,l2}]


{f[a1,b1],f[a2,b2],...}


There is no f[a1,b2] in this result. How can I get a full list that I want?

– Bill
Commented Aug 30, 2021 at 5:53
• I have considered the mapthread, but I didn't manage it. Could u show me the more details? Commented Aug 30, 2021 at 6:18
• Frankly, the MapThread documentation contains everything anyone could write in an answer here. Voting to close as "simple to find in the documentation". Commented Aug 30, 2021 at 6:33
• a = {a1, a2, a3}; b = {b1, b2, b3}; MapThread[f, {a, b}] gives {f[a1, b1], f[a2, b2], f[a3, b3]} and then EDIT: the question got changed.
– Syed
Commented Aug 30, 2021 at 6:39
• Thanks for the edit. Now it's clearer. Voted to reopen. Commented Aug 30, 2021 at 18:12

a = {a1, a2, a3};
b = {b1, b2, b3};


{f[a1, b1], f[a2, b2], f[a3, b3]}

How can I get a full list that I want?

What you are now looking for is called an outer product.

Flatten[Outer[List, a, b], 1]

{{a1, b1}, {a1, b2}, {a1, b3}, {a2, b1}, {a2, b2}, {a2, b3}, {a3,
b1}, {a3, b2}, {a3, b3}}

f @@@ Flatten[Outer[List, a, b], 1]

{f[a1, b1], f[a1, b2], f[a1, b3], f[a2, b1], f[a2, b2], f[a2, b3],
f[a3, b1], f[a3, b2], f[a3, b3]}

• Thank you very much! This is just the problem at beginning. Commented Aug 30, 2021 at 6:49
• @RuiYu This is not the proper way to ask a question. You cannot change your question every 2 minutes. Please return to the forum when you have formed the question.
– Syed
Commented Aug 30, 2021 at 6:51
• Thanks. I did not change my question. Maybe at beginning it is a little misleading. I will be more cautious next time. By the way, how to close a question? Commented Aug 30, 2021 at 6:55
• Users with "Close" vote privileges can close your questions. It's okay, no one minds helping you out, but it is in your interest to ask well-formed stable and non-trivial questions, otherwise responders will have lesser motivation to answer them.
– Syed
Commented Aug 30, 2021 at 7:01
• Flatten@Outer[f, a, b] Commented Aug 30, 2021 at 7:12
a = {a1, a2, a3};
b = {b1, b2, b3};

f @@@ Tuples[{a, b}]
(*    {f[a1, b1], f[a1, b2], f[a1, b3],
f[a2, b1], f[a2, b2], f[a2, b3],
f[a3, b1], f[a3, b2], f[a3, b3]}    *)

a = {a1, a2, a3}; b = {b1, b2, b3};

Tuples[f[a, b]]

{f[a1, b1], f[a1, b2], f[a1, b3],
f[a2, b1], f[a2, b2], f[a2, b3],
f[a3, b1], f[a3, b2], f[a3, b3]}


Also

Distribute[f[a, b], List]

{f[a1, b1], f[a1, b2], f[a1, b3],
f[a2, b1], f[a2, b2], f[a2, b3],
f[a3, b1], f[a3, b2], f[a3, b3]}

• These suggestions won't work if f aggressively executes right away and f[a, b] generates a result (instead of staying symbolic). Safer to use my solution, I think. Commented Jun 19 at 11:35