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

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:

Introduced in 2014
 (10.0)