Im trying to further my understanding of mathematica coding and came up across this question. What is the main difference between Cases and Select?

data = Table[{RandomReal[{-10, 10}], RandomReal[{-10, 10}], RandomReal[{-10, 10}]}, 10^4];
Cases[data, {a_, b_, c_} :> {a, b} /; c > 0]
Select[data, #[[3]] > 0 &][[All, ;; 2]]

Both these approaches give the same result, Im just curious about the benefits of each. When is Select superior to Cases and vice versa.


1 Answer 1


Cases uses patterns, Select uses testing functions. Because of this, Select is not seldomly faster because it reduces pattern matching to the very least: Does the testing function evaluate to True or not? (Pattern matching can be a real bottleneck sometimes, inparticular if the mere check for matching enforces unpacking of arrays.)

On the other hand, Cases is much more flexible.

1.) Cases allows a level specification. (Select always operates on the first level of an expression)

2.) Cases allows to apply replacement rules immediately.

  • $\begingroup$ Thanks @Alan. It was a typo. $\endgroup$ Sep 17, 2018 at 17:27

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