This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Pattern Matching  /
Position

  • Position[ expr , pattern ] gives a list of the positions at which objects matching pattern appear in expr.
  • Position[ expr , pattern , levspec ] finds only objects that appear on levels specified by levspec.
  • Example: Position[ 1+x^2, 5, x^4 , x^_].
  • Position[ expr , pattern ] tests all the subparts of expr in turn to try and find ones that match pattern.
  • Position returns a list of positions in a form suitable for use in Extract, ReplacePart and MapAt. The form is different from the one used in Part.
  • The default level specification for Position is {0, Infinity}, with Heads -> True.
  • Position[ list , pattern , 1 , Heads -> False] finds positions only of objects that appear as complete elements of list.
  • Level specifications are described in Section¬†A.3.6.
  • Position[ expr , pattern , levspec , n ] gives the positions of the first n parts of expr which match the pattern.
  • See the Mathematica book: Section 1.8.5, Section 2.1.7, Section 2.2.4,¬†Section 2.3.2.
  • See also: Cases, Count, StringPosition, ReplaceList, Insert, Delete.

    Further Examples

    The symbol c occurs in the third position of this list.

    In[1]:=

    Out[1]=

    This gives the positions of the elements of the list that are manifestly powers.

    In[2]:=

    Out[2]=

    The head of the expression (List in position 0) and the first two elements of the expression (a and Pi) have head Symbol.

    In[3]:=

    Out[3]=

    In[4]:=

    Out[4]=

    In[5]:=