# Selecting Values from a List Above a Certain Number

So lets say I create a list of pairs like so:

list = {{1, 100}, {2, 10000}, {3, 100400}, {4,50000}, {5,2000}}


Now, I want to find what entries satisfy the condition where the second number is larger than 10000. The code I have tried is as follows:

Select[list[[All, 2]], list[[All, 2]] > 10000]


I think I understand why the above code simply returns an empty set each time. In the conditional, it compares the entire list of the second column numbers to 10000, which does not make any sense. I want the conditional/criterion to compare each number in the second column of the list to 10000 separately, and then have the Select function return which values were true. Any ideas for how to do this?

• @Nasser's Cases and Select are great for ease-of-use. But you could also use something involving Pick, like Pick[#, UnitStep /@ (#[[;; , 2]] - 1000), 1] &@list, which can sometimes speed things up for long lists. Oct 18, 2017 at 3:12
• @aardvark2012 UnitStep is listable ;) Oct 18, 2017 at 13:28
• @LLlAMnYP Heh. Of course. Good answer! :-) Oct 18, 2017 at 23:32

list = {{1, 100}, {2, 10000}, {3, 100400}, {4, 50000}, {5, 2000}}
Cases[list,{_,y_}/;y>1000]


To use Select

Select[list,#[[2]]>1000&]


• Looks like that made it work. Thank you so much for your help! Oct 18, 2017 at 1:32

For completeness, the high-performance solution, similar to the comment by aardvark2012:

Pick[list, UnitStep[10000 - list[[;; , 2]]], 0]


A performance test:

list = RandomInteger[30000, {10^6, 2}];
(pick = Pick[list, UnitStep[10000 - list[[;; , 2]]], 0]) // AbsoluteTiming // First
(cases = Cases[list, {_, y_} /; y > 10000]) // AbsoluteTiming // First
(select = Select[list, #[[2]] > 10000 &]) // AbsoluteTiming // First
(pickUbq = Pick[list, #[[2]] > 10000 & /@ list]) // AbsoluteTiming // First
pick == cases == select == pickUBQ

0.050378
0.926318
1.72531
2.04373
True


Just example using:

Pick[list, #[[2]] > 1000 & /@ list]


and (more contrived) Reap/Sow:

Join @@ Reap[Sow[{##}, #2] & @@@ list, _?(# > 1000 &), #2 &][[-1]]

• Something seems wrong with the Reap/Sow solution. Is it giving sorted results? Oct 18, 2017 at 13:23
• @LLlAMnYP thank you ...will look at when I get a chance to sort out my error but it 23:30 in my TZ... Oct 18, 2017 at 13:26
• After inspection: As per my answer pick // Sort == yourReapSow // Sort. You're just sowing the list with tags equal to the second element, so of course they come in a different order. Otherwise same result. Oct 18, 2017 at 13:31