# Select elements in a list

I am new in Wolfram Math and I need a help in making a simple program. Let's suppose we have a list:

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};


Now, I need to collect the first elements of sublists, but not all of them, only those whose second elements are larger than 8.

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

Select[list, #[[2]] > 8 &][[All, 1]]


{1, 7, -1}

OR using Pick

Pick[list[[All, 1]], UnitStep[list[[All, 2]] - 8], 1]


{1, 7, -1}

• @Suro. You're welcome, glad I could help. Apr 18, 2016 at 11:43
• Wow! How about, when we say that list has 3 3l3mens in each sublist list = {{1, 11, 3}, {2, 7, 4}, {4, 2, 5}, {7, 9, 9}, {-2, 3, 3}, {-1, 10, 5}};. I would like to impose two conditions and get second and third element which satisfies this condition. How to do it? I wrote a code like this, but I don't know how to use it to get both 2nd and 3rd element: Select[list, #[[2]] > 8 && #[[3]] > 3 &] Select[list, #[[2]] > 8 && #[[3]] > 3 &][[All, 1]] Mar 16, 2020 at 15:37
list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};
Cases[list, {a_, b_} /; b > 8 :> a]
(* {1, 7, -1} *)


What I'm doing above is to use Cases to select only those sublists whose second element is greater than 8,

Cases[list, {a_, b_} /; b > 8]
(* {{1, 11}, {7, 9}, {-1, 10}} *)


The /; notation defines a Condition. Then I'm applying a :> to make a replacement, in this case keeping only the first element, look at RuleDelayed

• Wow! Another excellent answer. Now, lets say that we have list which is like this list={{1,11,3},{2,7,4},{4,2,5},{7,9,9},{-2,3,3},{-1,10,5}};. Now I have to take out the second and third value in the sublist which satisfies the criteria b>8 and c>8. I would like to take out the value of b and c which satisfies this criterion. How to do this? I wrote a code, but its not working: Cases[list,{a_,b_,c_}/;b>8:>b] Cases[list,{a_,b_,c_}/;b>8:>c] Cases[list,{a_,b_,c_}/;b>8&c>8:>b] Mar 16, 2020 at 15:49
Pick[list[[All, 1]], # > 8 & /@ list[[All, 2]]]


or

Pick[#[[1]], # > 8 & /@ #[[2]]] &@Transpose[list]


{1, 7, -1}

• Nice one. But, how about this list list={{1,11,3},{2,7,4},{4,2,5},{7,9,9},{-2,3,3},{-1,10,5}}; and taking out 2nd and 3rd element in each sublist which satisfies simultaneously 2 condition such that 2nd and 3rd element should be more than 8? Mar 16, 2020 at 15:54
• @sreerajt, you can try Pick[#, #[[1]] > 8 && #[[2]] > 8 & /@ #] &@list[[All, {2, 3}]] or Select[#[[1]] > 8 && #[[2]] > 8 &]@list[[All, {2, 3}]] or  Cases[_?(#[[1]] > 8 && #[[2]] > 8 &)]@list[[All, {2, 3}]].
– kglr
Mar 16, 2020 at 16:23
• many thank 4 dese excellent suggestions. But I do have a pblm in this: take 1st one. In this, what it does is dat it first picks out 2nd and 3rd elements from each sublist by list[[All,{2,3}]] and then apply the condition to check on 1st & 2nd element on this modified list (if you have a doubt you can check it by changing #[[2]] to #[[3]]-it gives an error). How about a case where u check for condition on 1st and 3rd element of each sublist & then select 2nd and 3rd element in d sublist which satisfies this condition? This question applies to all ur sugestions Mar 17, 2020 at 1:49

Mathematica/Wolfram Language provides a lot of flexibility. Some ways you could do this:

Cases[list, {x_, _?(# > 8 &)} :> x]
Select[list, #[[2]] > 8 &][[;; , 1]]
Pick[list, #[[2]] > 8 & /@ list][[;; , 1]]
True /. GroupBy[list, (#[[2]] > 8 &) -> (#[[1]] &)]
Flatten[Last@Reap[Sow @@@ list, _?(# > 8 &), #2 &]]


I encourage you to look the documentation and play. You can find the way that suits your aims and/or style.

and some more ridiculous:

Cases[{#1, Sign[#2 - 8]} & @@@ list, {x_, 1} :> x]
f[x_, y_] := x /; y > 8
f[__] := Sequence[]
f @@@ list

• Your advice is highly appreciated. Thanks.
– Suro
Apr 18, 2016 at 11:50

Using Region* functionality: (totally unnecessary here but ...)

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

reg = ImplicitRegion[y > 8, {x, y}];

Pick[list[[All, 1]], RegionMember[reg, list]]
Pick[list[[All, 1]], RegionWithin[reg, Point@#] & /@ list]


Result:

{1, 7, -1}

• (+1) Ingenious way of approaching the matter. Happy New Year, @Syed! :-) Jan 1 at 2:51

Using Position and Extract:

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

Extract[#, Position[#, {a_, b_} /; b > 8]] &@list

(*{{1, 11}, {7, 9}, {-1, 10}}*)


Or equivalently:

Extract[#, Position[#, x : {__} /; Last@x > 8]] &@list

(*{{1, 11}, {7, 9}, {-1, 10}}*)

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

f[n_][a_, b_] /; b > n := a
f[_][__] := Nothing

f[8] @@@ list


{1, 7, -1}

f[3] @@@ list


{1, 2, 7, -1}

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};


Using SequenceSplit (new in 11.3)

SequenceSplit[list, {{a_, b_}} /; b <= 8][[All, 1, 1]]


{1, 7, -1}