SelectFirst

SelectFirst[{e1,e2,},crit]

gives the first ei for which crit[ei] is True, or Missing["NotFound"] if none is found.

SelectFirst[{e1,e2,},crit,default]

gives default if there is no ei such that crit[ei] is True.

SelectFirst[crit]

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

Details

  • In SelectFirst[{e1,e2,},crit,default], default is only evaluated if there is no ei such that crit[ei] is True.
  • SelectFirst[crit][expr] is equivalent to SelectFirst[expr,crit].

Examples

open allclose all

Basic Examples  (4)

Select the first element that is even:

Use a pure function to test elements:

Give the default x when no even numbers are found:

SelectFirst operates on values in an Association:

Scope  (3)

Use the operator form of SelectFirst:

Find the first pair containing x:

Use a custom default value:

Generalizations & Extensions  (2)

SelectFirst works with any head, not just List:

SelectFirst works with SparseArray objects:

Properties & Relations  (4)

The default value is only evaluated if needed:

SelectFirst returns only the first match:

Select returns all matches:

SelectFirst can be implemented with a combination of Select and Replace:

SelectFirst is similar to FirstCase except that it uses a function instead of a pattern:

Select the first list that has a sum of elements less than 10:

Use FirstCase to get the same result:

Wolfram Research (2014), SelectFirst, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectFirst.html.

Text

Wolfram Research (2014), SelectFirst, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectFirst.html.

CMS

Wolfram Language. 2014. "SelectFirst." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SelectFirst.html.

APA

Wolfram Language. (2014). SelectFirst. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SelectFirst.html

BibTeX

@misc{reference.wolfram_2023_selectfirst, author="Wolfram Research", title="{SelectFirst}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/SelectFirst.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_selectfirst, organization={Wolfram Research}, title={SelectFirst}, year={2014}, url={https://reference.wolfram.com/language/ref/SelectFirst.html}, note=[Accessed: 19-March-2024 ]}