How to compare elements of lists with the same length?

Hi,

I struggle a little bit to find the best function to compare two equal length lists.
(should be compared the elements each by each, i.e a>d, b>e etc.)

list1={a,b,c} and list2{d,e,f,}

list1 > list2 ?

then I’d like to get a list of boolean like {TRUE,TRUE,FALSE} OR If{condition,“PASS”,“FAIL”}

Could you suggest the easiest way/function to do that?

Thank you

You can use the command Thread, try:

l1 = {a, b, c, d}; l2 = {e, f, g, h};
Thread[l1 > l2]
(* Out:   {a>e,b>f,c>g,d>h} *)

Regards – Henrik

Try:

Thread[list1>list2]

list1 or list2 can also be a single number…

Thank you, exactly this I was looking for …I tried “Map”, “Scan+Print”, “Condition” etc.

Thanks a lot

Note that you can do it in more than 1 way:

list1={1,2,3}
list2={2.5,2,1.5}
Thread[list1<list2]
Map[#[[1]]<#[[2]]&,{list1,list2}\[Transpose]]
Apply[#1<#2&,{list1,list2}\[Transpose],{1}]
Apply[Less,{list1,list2}\[Transpose],{1}]
Less@@@({list1,list2}\[Transpose])
MapThread[Less,{list1,list2}]
Table[list1[[i]]<list2[[i]],{i,Length[list1]}]

I came up with these ways! and there are probably more!

And if you want to know whether every element of list1 is greater than the corresponding element of list2, then include one more thing:

And @@ Thread[list1 > list2]

Though, generally faster would be:

AllTrue[{list1, list2}\[Transpose], Less @@ # &]

Because that one will stop after the first False is found…

Dear Gents,
It is so nice to see how this community works :wink:
Thanks for all the ideas, …of course I look for the most simplest one, and the below works well for me at the moment.

In[6]:= deal = Thread[list1 > list2]
Out[6]= {False, False, True}

However, I needed something after then the conditions was evaluated, and I was able to program only with one out put, like this

In[7]:= deal = Thread[list1 > list2] /. x_ /; x == True -> "PASS"
Out[7]= {False, False, "PASS"}

Could you please suggest maybe a way where I can pass 2 possible outputs from the boolean? Since I have false OR true output from “Thread[]” (but with condition I could pass only one output)
for instance: If x return false, then “FAIL” should be passed into the list

Thanks again

How about:

l1 = RandomInteger[2, 10];
l2 = RandomInteger[1, 10];
Thread[l1 > l2] /. {False -> "FAIL", True -> "PASS"}
(* Out (e.g.):   {"FAIL","FAIL","FAIL","PASS","PASS","PASS","FAIL","PASS","PASS", "PASS"}  *)

@Henrik, sorry for the late reply but I missed somehow the notification from the Wolfr community.

That was my wish to apply more output based on the boolean. Thanks a lot.

Best regards, Balazs

Hi everyone,

just for completeness if one is interested in speed:

l1 = RandomReal[1, 100000] - 0.5; l2 = RandomReal[1, 100000] - 0.5;
AbsoluteTiming[Positive[(l1 - l2)]]

where

AbsoluteTiming[Thread[l1 > l2]]

Speed might not be an issue though…

Cheers,

Marco

PS: For all the different approaches that Sander lists the respective values are:

I’m surprised by three things:

method 3 and 4 has a big difference for the same thing.

1 is slower than 6!

and that mapthread is the fastest…