This answer is not intended to show the most efficient method, but rather an alternative method that serves the pedagogical purpose of showing some important core functionality in Mathematica.
nixeagle's answer avoids explicitly testing every element of the list. If the test doesn't lend itself to inclusion in the third argument of Select
, then the below might be useful.
To do this, you need to learn about the standard Or
and And
functions, as well as the Map
(/@
) and Apply
(@@
) commands which are extremely important for any Mathematica user to learn. (see this tutorial)
Here is a simple example.
In[2]:= data = RandomInteger[{0, 10}, {10}]
Out[2]= {10, 1, 0, 10, 1, 5, 2, 2, 4, 1}
In[4]:= # > 5 & /@ data
Out[4]= {True, False, False, True, False, False, False, False, False, \
False}
In[6]:= And @@ (# > 5 & /@ data)
Out[6]= False
What is going on here is that you are mapping the function ("greater than 5") to each element of the list using Map
, to get a list of True/False values. You are then applying the standard logical function And
to the whole list to get the single Boolean value.
These are all very much core functionality in Mathematica and I recommend you read the documentation for these functions carefully and practice using them.
This is not the most efficient method, but for small problems you will not notice the difference.
In[11]:= Do[Select[data, ! # > 5 &, 1] === {}, {10000}] // Timing
Out[11]= {0.031, Null}
In[12]:= Do[And @@ (# > 5 & /@ data);, {10000}] // Timing
Out[12]= {0.11, Null}
For Exists
, the alternative to Select
would be MatchQ for patterns or MemberQ
for explicit values. The documentation has some useful examples.