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.

Details and OptionsDetails 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.
  • The default level specification for Position is {0, Infinity}, with Heads->True.
  • 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:
  • nlevels through n
    Infinitylevels through Infinity
    {n}level n only
    {n1,n2}levels through
  • 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 corresponds to the whole expression.
  • With the default option setting Heads->True, Position includes heads of expressions, and their parts.
  • Position traverses expressions in depth-first order, yielding lists of indices in lexicographic order.
New in 1
New to Mathematica? Find your learning path »
Have a question? Ask support »