Say you have two lists:

listA = {a, b, c}

listB = {d, e, f}

How would you produce a listC that equates each of the two's elements, ie.

listC = {a == d, b == e, c == f} ?

I know, it's very easy, but somehow I've already spent an slightly embarrassing amount of time on this. I suspect it will involve Map, but can't seem to get the elements to behave together per each element. It seems to just Append the two together for some reason.

SetAttributes[listA, Listable] SetAttributes[listB, Listable]

seems promising given this: http://reference.wolfram.com/mathematica/ref/Listable.html

  • 3
    $\begingroup$ Thread[listA == listB] $\endgroup$
    – rm -rf
    Jun 12 '13 at 0:45
  • $\begingroup$ @rm -rf thank you $\endgroup$
    – Ghersic
    Jun 12 '13 at 0:47
  • 1
    $\begingroup$ @rm-rf If listA is equal to listB, then Thread[listA == listB] will return just True. In that case, this: MapThread[Equal, {listA, listB}]. $\endgroup$
    – Michael E2
    Jun 12 '13 at 5:37
  • 1
    $\begingroup$ Related: Equating matrices (or higher order tensors) element-wise $\endgroup$ Jun 12 '13 at 7:43
  • 1
    $\begingroup$ Also related: (10211) (15556) $\endgroup$
    – Mr.Wizard
    Jan 18 '15 at 15:29

As @rm -rf mentioned in the comments,

Thread[listA == listB]

accomplishes what I'd hoped. Apoogies for missing this.

As Michael notes, if they are already equal to these values (you're not newly naming listA or listB) and they happen to already be equal , the query just returns "true." If this is the case, use:

MapThread[Equal, {listA, listB}]

as mentioned by MichaelE2 in the comments.

  • $\begingroup$ MapThread gives True/False. Any quick way to get them to 1 and 0? $\endgroup$ Nov 26 '17 at 21:41
  • $\begingroup$ @ChenStatsYu If I understand, you would like "True" to be indicated with a 1 and "False" with a 0? MapThread[Equal, {list1, list2}] /. {True :> 1, False :> 0} achieves this result. If it is something other than True/False, a conditional statement such as MapThread[~] /. {x_ /; x != True :> 0} should work, but doesn't - I think due to the unequal operator. However, some such conditional statement dependent upon the entity being replaced should suffice, or you can use Cases[~] or Position[MapThreads[~], True] to eliminate or detect, respectively, which statements were True. $\endgroup$
    – Ghersic
    Dec 5 '17 at 17:12
  • 1
    $\begingroup$ @ChenStatsYu Boole does this $\endgroup$
    – b3m2a1
    Nov 17 '19 at 22:16


Inner[Equal, listA, listB, List]
Equal @@@ Transpose[{listA, listB}]

{a == d, b == e, c == f}

  • $\begingroup$ How about setting variable list to equal values list variables = {"a", "b", "c", "d", "e"}, list = {0.745581, -0.412515, 0.701289, 0.666934, -0.559174}, list3 = Thread[variables = list], then IN a give OUT a ? $\endgroup$
    – SPIL
    Nov 14 '17 at 10:33
  • $\begingroup$ Years late "thanks," kglr. I did +1 back in 2013! SPIL, I'm not exactly sure what you're asking, but if you've a reasonably short list the notebook Frontend can display it all, skipping preliminary list definitions entirely and saying {a, b, c} = {1, 2, 3} will auto-thread Set over each pair. How to do this for sufficiently large lists that they must be represented internally is another question; it is probably doable via Part and Table or maybe Map. If setting a bunch of variables to a bunch of values is what you're after anyways... You will want to remove the quotes though. $\endgroup$
    – Ghersic
    Dec 5 '17 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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