I have a list with the following structure

list={ {x,y},{ {a1,b1},{a2,b3} } }

where the number of pairs {ai,bi} can be different. I would like to remove pairs {x,y} with y<0 and try to use


but this returns all elements of the list. What do I wrong?

Update: I have understood why my attempt is wrong but still do not understand how to remove these elements.

  • $\begingroup$ For the DeleteCases approach, I recommend using the level argument. e.g. DeleteCases[list, {_, b_} /; b < 0, Infinity] $\endgroup$
    – C. E.
    Commented Jun 11, 2020 at 10:09
  • $\begingroup$ but you can't do b<0 on symbol. $\endgroup$
    – Nasser
    Commented Jun 11, 2020 at 10:38
  • $\begingroup$ Are you removing the entire entry in the list if conditions are satisfied or just the {x,y} pair? $\endgroup$ Commented Jun 11, 2020 at 12:19
  • $\begingroup$ Artem, your initial DeleteCases method does work, perhaps your list structure used for this is not like how I define lst in my answer? $\endgroup$ Commented Jun 11, 2020 at 21:07

2 Answers 2


We can use a modification of the answer here to do this.

lst = {{{x1, -1}, {{a1, 1}, {c1, d1}}}, 
   {{x2, 1 }, {{a2, -1}, {c2, d2}}}, 
   {{x3, -1}, {{a3, 1}, {c3, d3}}},
   {{x4, 1}, {{a4, -1}, {c4, d4}}}};




Same output.

If you need to only remove the {x,y} pair, I would need to think about that.

Edit: I thought about it:


This way loses the ordering, but it works with Cases and shows a terrible way to pseudo-use multiple conditions:

  • 1
    $\begingroup$ I think this will remove pairs of {a,b} that satisfy the condition also. $\endgroup$ Commented Jun 11, 2020 at 12:17
  • 1
    $\begingroup$ This way works if you use {{_,_?NegativeQ},{__List}}. $\endgroup$ Commented Jun 11, 2020 at 14:50
  • $\begingroup$ The biggest problem for this answer to be valid is, that this question has Cases[list, _?NumberQ] is empty {}! So it can not be jugded by Mathematica this way meaningful. This correctly recognized by @CATrevillian! Otherwise this asked for an example from the Mathematica documentation: [reference.wolfram.com/language/ref/PatternTest.html] section Scope example 1: replace negative numbers with 0 or something or Nothing. From [reference.wolfram.com/language/ref/Nothing.html] section Properties and Relation are other concepts available. $\endgroup$ Commented Jun 12, 2020 at 9:04

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.