# Check if a list consists of numbers

I'd like to check if all elements of a list are numbers. I've tried

t = {5/4, 12}
MatrixQ[t, NumberQ]
MemberQ[t, NumberQ]
And @@ Table[NumberQ[t[[i]]], {i, 1, Length[t]}]


but only the last one yields the desired result. Is there a better way to check?

• VectorQ[t, NumberQ] or AllTrue[t, NumberQ] should do the trick - the VectorQ version only accepts lists of numbers, while the AllTrue version accepts any head Commented Jul 22, 2019 at 8:02
• Maybe ContainsOnly[t[[All, 0]], {Rational, Integer}] possibly with the addition of Real and/or Complex Commented Jul 22, 2019 at 8:15
• @LukasLang, Thanks, I like your answer the best. Sorry I cannot upvote it! Commented Jul 22, 2019 at 8:22
• Table[ Im[z] == 0, {z, t} ] Commented Jul 22, 2019 at 8:23
• I'm confused both by the question asking for Reals and NumberQ as if they were the same when they are not, and by the fact that Irrationals like $\pi$ and $e$ are not NumberQ. You are getting contradictory answers! Can you please edit and clarify? Commented Jul 22, 2019 at 8:34

You can use Element:

Element[{$$x_1 , x_2 , \ldots$$}, dom] asserts that all the $$x_i$$ are elements of dom.

Using mgamer's example list:

{Pi, 0.4, 1, 2/2, 1./3} ∈ Reals


True

The built-in mathematical constants:

{Catalan, °, E, EulerGamma, Glaisher, GoldenRatio, Khinchin, MachinePrecision, π} ∈ Reals


True

{Pi, 1 + I, 1, .5} ∈ Reals


False

• Seeing your solution I recognize, that I misinterpreted the original question. There was no question about Reals... ;-) Sometimes I see, what I want to see ;-) Commented Jul 23, 2019 at 15:56
• @mgamer, the original post was about reals:)
– kglr
Commented Jul 23, 2019 at 17:10
• :-)) Thank you! Reading the hidden comments is sometimes enlightening.... Commented Jul 24, 2019 at 19:13

Given a list:

list = {Pi, 0.4, 1, 2/2, 1./3}


you can do:

And @@ (Head[#] === Real & /@ list)
(* False *)


Using MatchQ and NumericQ:

t = {5/4, 12, Pi, 0.4};

MatchQ[t, {__?NumericQ}]

(*True*)


Another way to do this, nice and shorter (Thanks to @Syed!):

AllTrue[NumericQ][t]

• AllTrue[NumericQ][t]
– Syed
Commented Feb 4 at 11:37
• Nice and shorter, @Syed! :-) Commented Feb 4 at 16:05
list = {EulerGamma, Pi, 0.4, 1, 2/2, 1./3};


Since V 13.3 we have RealValuedNumberQ and RealValuedNumericQ

AllTrue[list, RealValuedNumericQ]


True

AllTrue[list, RealValuedNumberQ]


False (* because of EulerGamma *)