If I have
list1 = {1, 2, 3, 4}
list2 = {5, 6, 7, 8}
How can I do so, that
function[#1, #2]
takes #1
from list1
and #2
from list2
??
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIf I have
list1 = {1, 2, 3, 4}
list2 = {5, 6, 7, 8}
How can I do so, that
function[#1, #2]
takes #1
from list1
and #2
from list2
??
MapThread[function[#1, #2] &, {list1, list2}]
function
happens to be listable.
$\endgroup$
– Mr.Wizard
Jan 18 '15 at 19:39
Since your question leaves a wide range of possible interpretations I suggest to examine these methods
Inner[ f, list1, list2, List]
{f[1, 5], f[2, 6], f[3, 7], f[4, 8]}
or
f @@@ Transpose[{list1, list2}]
or
Thread[ f[ Transpose[{list1, list2}]]]
Perhaps you could prefer Outer
providing all possible pairs
Outer[ f, list1, list2]
{{f[1, 5], f[1, 6], f[1, 7], f[1, 8]}, {f[2, 5], f[2, 6], f[2, 7], 2, 8]}, {f[3, 5], f[3, 6], f[3, 7], f[3, 8]}, {f[4, 5], f[4, 6], f[4, 7], f[4, 8]}}
or
f @@@ Tuples[{list1, list2}]
{ f[1, 5], f[1, 6], f[1, 7], f[1, 8], f[2, 5], f[2, 6], f[2, 7], f[2, 8], 3, 5], f[3, 6], f[3, 7], f[3, 8], f[4, 5], f[4, 6], f[4, 7], f[4, 8]}