If I have a list:

list:={{1,1}, {2,2}, {3,3}, {4,"a"}}

And I would like to delete the row with a string character, the following works:

DeleteCases[list, {_, _?StringQ}]


{{1, 1}, {2, 2}, {3, 3}}

However, if I use:

DeleteCases[list, {_, _?\[Not]NumberQ}]

The result is

{{1, 1}, {2, 2}, {3, 3}, {4, a}}

I'm not sure how to generate the 'not' expression in the web interface, but the [Not] operator above is generated in Mathematica by -esc-not-esc. I thought I had effectively used the expression "not a numeric". In fact, StringQ identifies the element as a string and it is removed from the list. But isn't it also not NumberQ? So, check a single expression:


which returns


and, when I check

\[Not] NumberQ["a"]

the expression returns


That all seems to work as I expected.

Then, why doesn't my formulation of (not)NumberQ drop the row in DeleteCases? Precedence?


closed as off-topic by Szabolcs, Henrik Schumacher, halirutan Jun 15 '18 at 16:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, Henrik Schumacher, halirutan
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ !NumberQ is not a function _? must be followed by a function. You can use _?(Composition[Not,NumberQ]) or you can use x_ /; Not@NumberQ[x] $\endgroup$ – Szabolcs Jun 15 '18 at 15:36
  • $\begingroup$ Use _?(Not[NumberQ[#]]&) or _?(Not @* NumberQ) instead $\endgroup$ – Carl Woll Jun 15 '18 at 15:37
  • 1
    $\begingroup$ Also, a tip: ? has very high precedence. Parentheisise everything that comes after it. Even the Composition example above must be parethesised. $\endgroup$ – Szabolcs Jun 15 '18 at 15:37

The trouble here is that ? takes its right hand side and uses it as the head of a predicate. FullForm of \[Not] NumberQ is Not[NumberQ]. Making that the head, you get Not[NumberQ]["a"] for your string, when what you need is Not[NumberQ["a"]] to do what you want. The way to do it is to make an expression that's sensible as a head. The idiomatic way is:

DeleteCases[list, {_, _?(\[Not] NumberQ[#] &)}]

using a pure function as head, allowing you to insert the argument in the right part of the body. Alternatively, you can make a named definition:

nnq[x_] := Not[NumberQ[x]]
DeleteCases[list, {_, _?nnq}]

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