# Select from a list using another list of limits

Given a list of values

list1 = {2.4, 3.4, 5.9, 1.2, 9.5, 7.5}


and a another list containing theoretical maximum values

list2 = {3, 4, 6, 2, 9, 7}


I wish to select those values from list1 which are less than the corresponding maximum value. So from my example I would get the values

{2.4, 3.4, 5.9, 1.2}


Something like this (which doesn't work)

Select[list1, # < list2] &

• Pick[list1, Thread[Less[list1, list2]]]... – ciao Oct 2 '15 at 0:52
• @ciao ); I was about to post exactly the same !!! – SquareOne Oct 2 '15 at 0:53
• @SquareOne: That's probably the canonical way, and Pick[list1, UnitStep[Subtract[list1, list2]], 0] if speed matters for huge lists... – ciao Oct 2 '15 at 0:57
• Thank you ciao. I didn't know Pick existed. – Steven Anderson Oct 2 '15 at 0:58
• @SquareOne: Ah, these are really not "answer" material IMO, feel free to if you'd like. Second is faster for a couple of reasons: most times if you can do things arithmetically, you gain speed (intrinsic speed of simple math ops in MMA, under-the-covers parallelization/divide-n-conquer/etc.), and some operations (like Pick) use optimized strategies when they "know" things about the data structures - like a target has been Unitized or UnitSteped - so second form can be orders of magnitude faster on large lists - try it! – ciao Oct 2 '15 at 1:05

ciao's method is good.

Pick[list1, Thread[Less[list1, list2]]]

{2.4, 3.4, 5.9, 1.2}


Another -- perhaps more direct -- way to do it is

MapThread[If[#1 <= #2, #1, Nothing] &, {list1, list2}]

{2.4, 3.4, 5.9, 1.2}

Cases[Transpose[{list1, list2}], {a_, b_} /; a < b :> a]


{2.4, 3.4, 5.9, 1.2}

• what does the a <b:>a do in your logic? I see that it works I just don't understand how. – Steven Anderson Oct 14 '15 at 14:28