# Conditional deleting pairs inside a list of pairs [duplicate]

I have a list of pairs for example like following:

list={{1,0.001},{10^(-7),0.001},{0.01,0.00005},{10^(-11),10^(-4)}}


Now I would like to delete all pairs (the complete pair), where the first element of the pair is smaller than 10^(-6). I tried some complicated combination of a For-Next loop and an If condition but without success. Is there an elegant way of doing this?

Luke

edit: extension: How would you do it, if I would have a list of lists of pairs? (Again delete the complete pair if the first element is smaller than some number)

• DeleteCases[list, {a_, _} /; a <= 10^(-6)] Feb 19, 2016 at 17:41
• Select[list, First@# > 10^(-6) &] Feb 19, 2016 at 17:42
• GroupBy[list, First@# > 10^(-6) &][True] Feb 19, 2016 at 17:44
• Wow mathematica is amazing :) Thank you alot! How would you extend this if I would have a list of lists of pairs? (again delete the pairs if the first element is smaller than 10^(-5))
– Luke
Feb 19, 2016 at 17:45
• list /. {a_ /; a <= 10^(-6), _} :> (## &[]) Feb 19, 2016 at 17:47

An easy new way (since Mathematica 10.2) to remove some part from a list or expression is to replace that part with Nothing

For example you can do

list = {{1, 0.001}, {10^(-7), 0.001}, {0.01, 0.00005}, {10^(-11),
10^(-4)}};
list /. {a_ /; a <= 10^-6, _} -> Nothing


to get:

{{1, 0.001}, {0.01, 0.00005}}

• /. is ReplaceAll
• ... -> ... is Rule
• {a_ /; a <= 10^-6, _} is a pattern with a Condition; you can read it "any pair with some first element/expression (say a) such that a<=10^-6 and any second element (_ is Blank, any expression)"

The good (or, sometimes, maybe the bad) about this method is that easily translates to the "list of list (of list of list...) of pair". You don't need to change anything:

{list, list, list} /. {a_ /; a <= 10^(-6), _} -> Nothing


{{{1, 0.001}, {0.01, 0.00005}}, {{1, 0.001}, {0.01, 0.00005}}, {{1, 0.001}, {0.01, 0.00005}}}

This method it's just a variant of the "fun" approach of @march, but with an easy-to-understand (?) syntax, available since v10.2. Under a previous you still need to use an equivalent approach:

... /. {a_ /; a <= 10^(-6), _} -> Sequence[]
... /. {a_ /; a <= 10^(-6), _} -> (##&[]) (as of @march comment)


Using Nothing, Sequence[], or (##&[]) and :> or -> is not always the same, but, for this sample, it is.

Pick[list, UnitStep[list[[All, 1]] - 10^-6], 1]


{{1, 0.001}, {0.01, 0.00005}}

not useful in the list of list of pair scenario and not exactly equivalent to the previous, but can have some other advantage:

list2 = Flatten[Table[list, {10000}], 1];
Pick[list2, UnitStep[list2[[All, 1]] - 10^-6], 1] //
RepeatedTiming // First
list2 /. {a_ /; a <= 10^-6, _} -> Nothing // RepeatedTiming // First


0.0338

0.0580

• The way to do it pre-10.2 is list /. {a_ /; a <= 10^(-6), _} :> (## &[]), as in my comment above. I appreciate you writing it out in detail. +1. Feb 19, 2016 at 22:14