Mathematica 9 is now available
 Documentation /  Mathematica /  The Mathematica Book /  Principles of Mathematica /  Functional Operations /

Building Lists from FunctionsExpressions with Heads That Are Not Symbols

2.2.7 Selecting Parts of Expressions with Functions

Section 1.2.4 showed 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.

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

This selects the elements of the list for which the pure function yields True, i.e., those numerically greater than 4.

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

Out[1]=

You can use 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.

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

Out[2]=

You can use Select to pick out only those terms in the sum that do not involve the symbol x.

In[3]:= Select[t, FreeQ[#, x]&]

Out[3]=

Selecting pieces of expressions.

Section 2.3.5 discusses some "predicates" that are often used as criteria in Select.

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

In[4]:= Select[{-1, 3, 10, 12, 14}, # > 3 &, 1]

Out[4]=

Building Lists from FunctionsExpressions with Heads That Are Not Symbols



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.