3
$\begingroup$

I have a list like the following:

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

I would like to select those entries whose second elements are greater than 2, therefore the result should be:

newlist = {{1,11},{2,7},{7,9}}

I know how to doit separately with:

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

but in that way I get {1, 2, 7} and {11, 7, 9}. How could I join this two output to get: newlist = {{1,11},{2,7},{7,9}}

Thank you in advance,

$\endgroup$

3 Answers 3

4
$\begingroup$

Try

Select[list, #[[2]] > 2 &]
(*{{1, 11}, {2, 7}, {7, 9}}*)   
$\endgroup$
2
$\begingroup$
list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 1}};

Pre-define pattern for better readability

p = {_, a_} /; a <= 2;

Now there are many possibilities ...

DeleteCases[list, p];

list /. p :> Nothing;

Catenate @ SequenceSplit[list, {p}];

Delete[Position[list, p]] @ list;

All return

{{1, 11}, {2, 7}, {7, 9}}

$\endgroup$
1
$\begingroup$
list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 1}};

Using Replace at level 1:

Replace[list, {_, b_ /; b <= 2} :> Nothing, {1}]

(*{{1, 11}, {2, 7}, {7, 9}}*)

Or using Pick:

Pick[#, Map[#[[2]] > 2 &]@#] &@list

(*{{1, 11}, {2, 7}, {7, 9}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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