# Position

Position[expr,pattern]

gives a list of the positions at which objects matching pattern appear in expr.

Position[expr,pattern,levelspec]

finds only objects that appear on levels specified by levelspec.

Position[expr,pattern,levelspec,n]

gives the positions of the first n objects found.

Position[pattern]

represents an operator form of Position that can be applied to an expression.

# Details and Options

• 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.
• Position looks only for matches to values in Association objects that appear in expr. It returns any part specifications within Association objects in the form Key[k].
• The default level specification for Position is {0,Infinity}, with .
• A part specification {} returned by Position represents the whole of expr.
• Position[list,pattern,{1},Heads->False] finds positions only of objects that appear as complete elements of list.
• Position uses standard level specifications:
•  n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2
• A positive level n consists of all parts of expr specified by n indices.
• A negative level -n consists of all parts of expr with depth n.
• Level 0 corresponds to the whole expression.
• With the default option setting , Position includes heads of expressions and their parts.
• Position traverses expressions in depthfirst order, yielding lists of indices in lexicographic order.
• Position[pattern][expr] is equivalent to Position[expr,pattern].

# Examples

open allclose all

## Basic Examples(4)

Find the positions at which b occurs:

 In[1]:=
 Out[1]=
 In[2]:=
 Out[2]=

Find all positions at which x to any power appears:

 In[1]:=
 Out[1]=

Find only those down to level 2:

 In[2]:=
 Out[2]=

Find positions in an Association:

 In[1]:=
 Out[1]=

Find positions with prime values:

 In[1]:=
 Out[1]=