Consider a list such as

s = {1, 2, 3, 3, 5, 6, 3}

With IntegerQ[number] , you know if number is an Integer, the result is True, and if not, the result is False.

But how could I do this without visiting each element as in Do[ ..., {i, 1, Length[...]}]?


closed as off-topic by Kuba, Jens, m_goldberg, RunnyKine, ciao Jun 8 '14 at 0:31

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." – Kuba, Jens, m_goldberg, RunnyKine, ciao
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ @kuba, Yes I have viewed the documentation, but I can´t reach the solution. I know that was simple for people who know it. Not for me. $\endgroup$ – Mika Ike Jun 7 '14 at 15:25
  • 1
    $\begingroup$ I looked for a duplicate, but the closest I found was question 8650; in particular, this answer by Mr.Wizard uses VectorQ in the way my answer does. $\endgroup$ – Michael E2 Jun 7 '14 at 19:07
  • $\begingroup$ I don't know how easily the best answer is found, but ArrayQ is used to test for a vector of integers here in documentation for IntegerQ, and one can find VectorQ here in the documentation for ArrayQ. $\endgroup$ – Michael E2 Jun 8 '14 at 12:35
  • 1
    $\begingroup$ Somewhat related: 916, 7120, 16694 $\endgroup$ – Michael E2 Jun 8 '14 at 12:36
s = {1,2,3,3,5,6,3};

I would write:

And @@ (IntegerQ /@ s)


With V10 we can use:

AllTrue[s, IntegerQ]


NoneTrue[s // N, IntegerQ]



VectorQ is the best way I know. Here are three types of input, packed array of Integer, an (non-packed) array of Integer, and an array of not all Integer.

packed   = RandomInteger[10, 10^7];
unpacked = Flatten@ConstantArray[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10^6];
nonint   = ReplacePart[packed, 10^6 -> 1.5];

(* False *)

Testing VectorQ[list, IntegerQ]:

VectorQ[packed, IntegerQ]
VectorQ[unpacked, IntegerQ]
VectorQ[nonint, IntegerQ]

Comparing the timings with two other methods (the timing function timeAvg is given below):

VectorQ[packed, IntegerQ] // timeAvg
VectorQ[unpacked, IntegerQ] // timeAvg
VectorQ[nonint, IntegerQ] // timeAvg

MatchQ[packed, {___Integer}] // timeAvg
MatchQ[unpacked, {___Integer}] // timeAvg
MatchQ[nonint, {___Integer}] // timeAvg

And @@ (IntegerQ /@ packed) // timeAvg
And @@ (IntegerQ /@ unpacked) // timeAvg
And @@ (IntegerQ /@ nonint) // timeAvg

You may note that on packed arrays VectorQ and MatchQ take virtually no time. In fact, it's the same amount of time no matter what the size. This is because a packed array is a special efficient internal representation of an array. In particular it has to be an array of all the same type of number (only Integer, Real, and Complex are allowed). So checking the type is easy. See What is a Mathematica packed array?

The site-standard timeAvg function:

SetAttributes[timeAvg, HoldFirst]
timeAvg[func_] := 
  Do[If[# > 0.3, Return[#/5^i]] & @@ 
    AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];
  • $\begingroup$ My function seems to unpack, whereas yours circumvents this nasty habit. Thanks, very instructive. $\endgroup$ – eldo Jun 7 '14 at 19:35
  • $\begingroup$ @eldo Thanks. I think the OP's use-case is unlikely to have a packed list, since it seems possible that not all entries are integers. (Indeed, it must be rare that one would need to do such a test on a packed array.) It appears that mapping IntegerQ over the list is the rate-limiting step. $\endgroup$ – Michael E2 Jun 7 '14 at 19:55
  • $\begingroup$ I too like VectorQ to prevent unpacking. +1 $\endgroup$ – Mr.Wizard Jun 8 '14 at 13:32

Use Map, abbreviated /@. For example:

  lis = {4, 5/2, 3., 6/3, Pi};
  IntegerQ /@ lis               (* or Map[IntegerQ, lis] *)

(* {True, False, False, True, False} *)


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