FirstPosition
✖
FirstPosition
gives the position of the first element in expr that matches pattern, or Missing["NotFound"] if no such element is found.
finds only objects that appear on levels specified by levelspec.
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 allclose allBasic Examples (3)Summary of the most common use cases
Find the first position at which b occurs:

https://wolfram.com/xid/0d6fke9ziq-v4f


https://wolfram.com/xid/0d6fke9ziq-nz7


https://wolfram.com/xid/0d6fke9ziq-j9ssb0

Find the first position at which x to any power appears:

https://wolfram.com/xid/0d6fke9ziq-fwf

Find the first position in an Association:

https://wolfram.com/xid/0d6fke9ziq-fpp3ld

Scope (4)Survey of the scope of standard use cases

https://wolfram.com/xid/0d6fke9ziq-836t6x

Find the first position with a prime value:

https://wolfram.com/xid/0d6fke9ziq-dqvhf

Specify a custom default value:

https://wolfram.com/xid/0d6fke9ziq-bswqzv


https://wolfram.com/xid/0d6fke9ziq-8z0iuc

Look for only a 3 that appears as an element of the outermost list:

https://wolfram.com/xid/0d6fke9ziq-547dz3

Options (1)Common values & functionality for each option
Heads (1)
By default, FirstPosition will search expression heads:

https://wolfram.com/xid/0d6fke9ziq-szy

Use the option Heads->False to exclude them:

https://wolfram.com/xid/0d6fke9ziq-ld9

Properties & Relations (3)Properties of the function, and connections to other functions
The default value is only evaluated if needed:

https://wolfram.com/xid/0d6fke9ziq-ikdop5


https://wolfram.com/xid/0d6fke9ziq-f6ohyb

Use Extract to extract parts based on results from FirstPosition:

https://wolfram.com/xid/0d6fke9ziq-v58

https://wolfram.com/xid/0d6fke9ziq-nju


https://wolfram.com/xid/0d6fke9ziq-e1i

FirstPosition returns only the first position:

https://wolfram.com/xid/0d6fke9ziq-18dbdy

Position returns all positions:

https://wolfram.com/xid/0d6fke9ziq-odqxnc

FirstPosition can be implemented with a combination of Position and Replace:

https://wolfram.com/xid/0d6fke9ziq-nnbjkg



https://wolfram.com/xid/0d6fke9ziq-iftb30


Possible Issues (1)Common pitfalls and unexpected behavior
FirstPosition searches based on patterns, which may not be the same as numerical equality:

https://wolfram.com/xid/0d6fke9ziq-srm

Write a pattern using Condition to force searching based on numerical equality:

https://wolfram.com/xid/0d6fke9ziq-1u062z

Wolfram Research (2014), FirstPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/FirstPosition.html.
Text
Wolfram Research (2014), FirstPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/FirstPosition.html.
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.
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
Wolfram Language. (2014). FirstPosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FirstPosition.html
BibTeX
@misc{reference.wolfram_2025_firstposition, author="Wolfram Research", title="{FirstPosition}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/FirstPosition.html}", note=[Accessed: 31-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_firstposition, organization={Wolfram Research}, title={FirstPosition}, year={2014}, url={https://reference.wolfram.com/language/ref/FirstPosition.html}, note=[Accessed: 31-May-2025
]}