4
$\begingroup$

This question already has an answer here:

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??

$\endgroup$

marked as duplicate by Kuba, Mr.Wizard Jan 18 '15 at 19:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

8
$\begingroup$
MapThread[function[#1, #2] &, {list1, list2}]
$\endgroup$
  • 7
    $\begingroup$ No need for: [#1, #2] & $\endgroup$ – Kuba Jan 18 '15 at 14:36
  • $\begingroup$ @David No, that's not the same, unless function happens to be listable. $\endgroup$ – Mr.Wizard Jan 18 '15 at 19:39
8
$\begingroup$

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]}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.