# How to check if all the members of list lies in specific range

I have a list the form:

myList = {{0.12}, {0.12}, {0.12}, {0.12}, {0.14}, {0.12}, {0.14}, {0.12}, \
{0.14}, {0.12}, {0.12}, {0.12}, {0.12}, {0.12}, {0.14}, {0.12}, \
{0.14}, {0.12}, {0.14}, {0.12}, {0.12}, {0.14}, {0.12}}


How can I check if all its elements are in range .10<x<.15. It would be nice to have true or false as the answer.

With my limited knowledge,I have tried

AllTrue[myList,x_ /; .10<x<.15,3]


but I am not quite optimistic about it!!

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Commented Apr 16, 2015 at 15:57

AllTrue[Flatten@myList, .1 < # < .15 &]


(* True *)

which @kglr finds is the fastest.

• Thanks David and Picket for the quick reply... Commented Apr 16, 2015 at 15:43
f = Through@{Min, Max}@## == {##2} &;

f[myList, .1, .15]
(* True *)


Update: Operator form of AllTrue:

AllTrue[.1 <= #[[1]] <= .15&]@myList
(* True *)


Update 2: some timings of the methods proposed so far:

functions = {"Through@{Min, Max}@## == {##2} &[list, min,max]",
"AllTrue[min<=#[[1]]<=max&]@list",
"AllTrue[min<=#<=max&]@Flatten[list]",
"AllTrue[Flatten@list, min<= # <=max &]",
"VectorQ[list, min <= First@# <= max &]",
"And @@ Flatten@IntervalMemberQ[Interval[{min, max}], list]",
"And @@ Flatten@Function[x, min <= x <=max, Listable]@list",
"And @@ MatchQ[x_ /; min <= x <=max] @@@list",
"And @@ Thread[min <= Flatten@list <=max]",
"(#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@list]",
"(Min[Flatten@rlst] >= min && Max[Flatten@list] <= max )"};

rlst = List /@ RandomInteger[100, 1000000];

{min,max}={0,100};
Grid[Transpose[{functions,{f[rlst, min, max] // AbsoluteTiming ,
AllTrue[min<=#[[1]]<=max&]@rlst// AbsoluteTiming,
AllTrue[min<=#<=max&]@Flatten[rlst]// AbsoluteTiming,
AllTrue[Flatten@rlst, min<= # <=max &]// AbsoluteTiming,
VectorQ[rlst, min <= First@# <= max &] // AbsoluteTiming,
And @@ Flatten@IntervalMemberQ[Interval[{min,max}], rlst]// AbsoluteTiming,
And @@ Flatten@Function[x, min <= x <=max, Listable]@rlst// AbsoluteTiming,
And @@ MatchQ[x_ /;  min <= x <=max] @@@rlst // AbsoluteTiming,
And @@ Thread[min<= Flatten@rlst <=max]// AbsoluteTiming,
(#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@rlst]//AbsoluteTiming,
(Min[Flatten@rlst] >= min && Max[Flatten@rlst] <= max )//AbsoluteTiming}}],
Dividers->All]


{min,max}={0,100} {min, max} = {0, 99} {min, max} = {20, 70}

• one more: (#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@rlst] or even (Min[Flatten@rlst] >= min && Max[Flatten@rlst] <= max ) .. Commented Apr 16, 2015 at 18:54
• @george2079, I will add the two functions you suggest to the timing experiment -- it may take a day or so (i am using the free programming cloud version and i might have exceeded my daily quota today). Re Through , it is checking the {Min,Max} of Sequence[list, min, max] (not that of list; note the ##) against {min,max}.
– kglr
Commented Apr 16, 2015 at 19:11
• yea, I just figured that out, slick. Commented Apr 16, 2015 at 19:13

A few alternatives

VectorQ[myList, 0.1 < First@# < 0.15 &]
(* True *)

And @@ Flatten@IntervalMemberQ[Interval[{0.1, 0.15}], myList]
(* True *)

And @@ Flatten@Function[x, 0.10 < x < 0.15, Listable]@myList
(* True *)

And @@ MatchQ[x_ /; 0.1 < x < 0.15] @@@ myList
(* True *)

• Personally I like And @@ Thread[10 < myList < 15] Commented Apr 16, 2015 at 16:27
• @2012rcampion Nice, for the OP's data you need Flatten as well And @@ Thread[0.1 < Flatten@myList < 0.15] but it's still short and concise. Commented Apr 16, 2015 at 16:37

Just for variety:

in[lst_, a_, b_] :=
Min[Sign[(# - a) (b - #)] & /@lst] /. {1 -> True, _ ->
False}


so,

in[Flatten@myList, 0.1, 0.15]


yields True

and in[Flatten@myList, 0.02, 0.1] yields False.

Small modification for closed or half open/closed...

MinMax came with V 10.1 and Between with 10.3

And @@ Map[Between[{.12, .15}]] @ MinMax[myList]


True