Selecting Parts of Expressions with Functions

"Manipulating Elements of Lists" shows how you can pick out elements of lists based on their positions. Often, however, you will need to select elements based not on where they are, but rather on what they are.

(RefLink[Select, paclet:ref/Select])[list,f] selects elements of list using the function f as a criterion. (RefLink[Select, paclet:ref/Select]) applies f to each element of list in turn, and keeps only those for which the result is (RefLink[True, paclet:ref/True]).

This selects the elements of the list for which the pure function yields (RefLink[True, paclet:ref/True]), i.e., those numerically greater than 4:

Select[{2,15,1,a,16,17},#>4&]

You can use (RefLink[Select, paclet:ref/Select]) to pick out pieces of any expression, not just elements of a list.

This gives a sum of terms involving x, y, and z:

t=Expand[(x+y+z)^2]

You can use (RefLink[Select, paclet:ref/Select]) to pick out only those terms in the sum that do not involve the symbol x:

Select[t,FreeQ[#,x]&]

Selecting pieces of expressions.

"Putting Constraints on Patterns" discusses some "predicates" that are often used as criteria in (RefLink[Select, paclet:ref/Select]).

This gives the first element which satisfies the criterion you specify:

Select[{-1,3,10,12,14},#>3&,1]