Consider the following:

list={1/First[{}], 1, 2, 1/First[{}], 3};

I wanted to remove all cases, which did not match _!NumberQ (e.g. 1/First[{}]), in the first place. But after DeleteCases[list,_!NumberQ] did not work, I tried it with DeleteCases[list,_NumberQ], just to see whether that would work...didn't.

What I am doing wrong?

  • 2
    $\begingroup$ NumberQ is a predicate function so to make a pattern you need _?NumberQ $\endgroup$ – Andrzej Kozlowski Mar 25 '12 at 17:35
  • $\begingroup$ just curious... is there a reason you unaccepted mine? $\endgroup$ – rm -rf Mar 25 '12 at 17:56
  • $\begingroup$ R.M., sorry, I thought I did. Sorry! $\endgroup$ – John Mar 25 '12 at 19:59
  • 1
    $\begingroup$ @John Have you considered if one of the answers can be accepted ? I would suggest to accept R.M.'s answer, but you can do whatever you would like and I think the both answers give good solutions to the problem. $\endgroup$ – Artes Apr 29 '12 at 21:17

The syntax _foo indicates that you're looking for a pattern with the head foo. NumberQ is not a Head, but a test returning a boolean True or False depending on whether the expression is a number or not. So you'd have to use it with PatternTest as _?NumberQ. For your example, the following should work:

Cases[list, _?NumberQ]

If you wanted to stick with DeleteCases, then you'll have to negate the test using either of the three constructs below:

DeleteCases[list, _?(Composition[Not, NumberQ])]
DeleteCases[list, _?(! NumberQ[#] &)]
DeleteCases[list, Except[_?NumberQ]]

Beware that ? has a very high precedence and binds very tightly and hence the parentheses are necessary in the first two constructs. See this question for more info.

  • $\begingroup$ Here another DeleteCases-issue: {{"Bla1","Bla2","Bla3","Bla4"}, {-0.81682, 0.373522}, {0.373522, -0.451794}} DeleteCases[%, {# > 0, # < 0} &] I fear, that I still have a great lack of basic knowledge... :-/ $\endgroup$ – John Mar 25 '12 at 19:59
  • $\begingroup$ @John This example better fits to use of Select, look at my answer. $\endgroup$ – Artes Mar 25 '12 at 20:26
  • $\begingroup$ @Artes: This is really odd. I swear I didn't unaccepted it. Sorry, though. $\endgroup$ – John Mar 25 '12 at 20:52
  • 1
    $\begingroup$ @John I think you're confused here. You can "accept" only one answer, not both. When you check more than one answer, the checkmark gets removed from the previous answer, thereby "unaccepting" it. You can see this in the timeline, where you first accepted mine, then Artes', then mine again and back to Artes'. Generally, people accept the answer that is clear and was helpful to them. In cases of competing answers, different users have different ways to decide whose to pick — votes/clarity/elegance/coin-toss... You gotta pick yours :) $\endgroup$ – rm -rf Mar 25 '12 at 21:43
  • $\begingroup$ Just a small nitpicking here: NumberQ is a head, just like any other symbol, e.g. see: Quiet@Cases[{2, NumberQ[], 1, NumberQ[1]}, _NumberQ]. $\endgroup$ – István Zachar Mar 26 '12 at 8:36

Consider the following :

Cases[list, _?NumberQ]


DeleteCases[list, Except[_?NumberQ]]

you could also use :

DeleteCases[list, Except[_?NumericQ]]


The reason one misuses DeleteCases is because there are similar constructs which work like this (giving the same result) :

Select[list, NumberQ]

i.e. Select[list, criterion] picks out elements of the list for which criterion is True unlike DeleteCases[list, pattern] which removes elements of list that match given a pattern. DeleteCases is supposed to work with expressions, while Select works basically with lists, although list can have any head, not only List.

NumberQ is a more restictive function than NumericQ, since the latter gives True also for symbols like e.g. E :

Head /@ {E, N[E]}

{Symbol, Real}

{NumberQ[E], NumericQ[E]}

{False, True}

  • 1
    $\begingroup$ NumberQ and NumericQ are not the same. For example: Through[{NumberQ, NumericQ}@π] $\endgroup$ – rm -rf Mar 25 '12 at 17:55
  • $\begingroup$ NumberQ and NumericQ do the same work for this kind of list, however they may yield different results in general. $\endgroup$ – Artes Mar 25 '12 at 18:03
  • $\begingroup$ I never said you should remove your answer :) I merely observed that identical solutions were posted merely 2 seconds apart. I've never had such a close call, although I've seen closer $\endgroup$ – rm -rf Mar 25 '12 at 18:06
  • $\begingroup$ I don't find your answers unhelpful at all! I think I've voted for most of them. $\endgroup$ – rm -rf Mar 26 '12 at 0:54
  • $\begingroup$ @R.M Thank You ! Nevertheless I see I could improve most of my answers but in general I try to avoid to many edits. On the other hand I learn mainly when I try to give answers, even though I don't have them right now. $\endgroup$ – Artes Mar 26 '12 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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