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.
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 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,default,{1},Heads->False] finds positions only of objects that appear as complete elements of list.
- FirstPosition 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 Heads->True, FirstPosition includes heads of expressions and their parts.
- FirstPosition traverses expressions in depth‐first order.
- FirstPosition[pattern][expr] is equivalent to FirstPosition[expr,pattern].
Examples
open all close allBasic Examples (3)
Find the first position at which b occurs:
FirstPosition[{a, b, a, a, b, c, b}, b]FirstPosition[{{a, a, b}, {b, a, a}, {a, b, a}}, b]FirstPosition[{x, y, z}, b]Find the first position at which x to any power appears:
FirstPosition[{1 + x ^ 2, 5, x ^ 4, a + (1 + x ^ 2) ^ 2}, x ^ _]Find the first position in an Association:
FirstPosition[<|{1 -> 1 + x ^ 2, 2 -> <|"a" -> x ^ 2|>, 3 -> x ^ 4, 4 -> a + (1 + x ^ 2) ^ 2}|>, x ^ _]Scope (4)
FirstPosition[_Integer][{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}]Find the first position with a prime value:
FirstPosition[<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, _Integer ? PrimeQ]Specify a custom default value:
FirstPosition[{1, 2, 3}, _ ? StringQ, "NoStrings"]FirstPosition[{{1, 2}, {2, 3}, {3, 1}}, 3]Look for only a 3 that appears as an element of the outermost list:
FirstPosition[{{1, 2}, {2, 3}, {3, 1}}, 3, Missing["NotFound"], {1}]Options (1)
Heads (1)
By default, FirstPosition will search expression heads:
FirstPosition[x ^ 2 + y ^ 2, Power]Use the option Heads->False to exclude them:
FirstPosition[x ^ 2 + y ^ 2, Power, Heads -> False]Properties & Relations (3)
The default value is only evaluated if needed:
FirstPosition[{1015177, "Junk", EndOfFile}, _ ? PrimeQ, Print["CriticalError"];$Failed]FirstPosition[{1015177, 2015177, 3015177}, _ ? PrimeQ, Print["CriticalError"];$Failed]Use Extract to extract parts based on results from FirstPosition:
u = {1 + x^2, 5, x^4, a + (1 + x^2)^2};FirstPosition[u, x ^ _]Extract[u, %]FirstPosition returns only the first position:
FirstPosition[{a, b, b, a, a, c}, a]Position returns all positions:
Position[{a, b, b, a, a, c}, a]FirstPosition can be implemented with a combination of Position and Replace:
FirstPosition[{a, b, b, a, a, c}, b]
FirstPosition[{a, b, b, a, a, c}, d]Replace[Position[{a, b, b, a, a, c}, b], {{} :> Missing["NotFound"], l_ :> First[l]}]
Replace[Position[{a, b, b, a, a, c}, d], {{} :> Missing["NotFound"], l_ :> First[l]}]Possible Issues (1)
FirstPosition searches based on patterns, which may not be the same as numerical equality:
FirstPosition[Range[-1, 1, 0.05], 0.1]Write a pattern using Condition to force searching based on numerical equality:
FirstPosition[Range[-1, 1, 0.05], n_ /; n == 0.1]Related Guides
History
Text
Wolfram Research (2014), FirstPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/FirstPosition.html.
CMS
Wolfram Language. 2014. "FirstPosition." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FirstPosition.html.
APA
Wolfram Language. (2014). FirstPosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FirstPosition.html
BibTeX
@misc{reference.wolfram_2026_firstposition, author="Wolfram Research", title="{FirstPosition}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/FirstPosition.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_firstposition, organization={Wolfram Research}, title={FirstPosition}, year={2014}, url={https://reference.wolfram.com/language/ref/FirstPosition.html}, note=[Accessed: 12-June-2026]}