FirstPosition

FirstPosition[expr,pattern]
gives the position of the first element in expr that matches pattern, or Missing["NotFound"] if no such element is found.

FirstPosition[expr,pattern,default]
gives default if no element matching pattern is found.

FirstPosition[expr,pattern,default,levelspec]
finds only objects that appear on levels specified by levelspec.

FirstPosition[pattern]
represents an operator form of FirstPosition that can be applied to an expression.

Details and OptionsDetails and Options

  • In FirstPosition[expr,pattern,default], default is evaluated only if no element matching pattern is found.
  • The expression expr can be a list, an association, or any other type of expression. In the case of an association, FirstPosition gives a key.
  • The default level specification for FirstPosition is {0,Infinity}, with Heads->True.
  • A part specification returned by FirstPosition represents the whole of expr.
  • FirstPosition[list,pattern,{1},Heads->False] finds positions only of objects that appear as complete elements of list.
  • FirstPosition 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, FirstPosition includes heads of expressions and their parts.
  • FirstPosition traverses expressions in depthfirst order.
  • FirstPosition[pattern][expr] is equivalent to FirstPosition[expr,pattern].
Introduced in 2014
(10.0)