FirstCase[{e1,e2,…},pattern]
gives the first ei to match pattern, or Missing["NotFound"] if none matching pattern is found.
FirstCase[{e1,…},patternrhs]
gives the value of rhs corresponding to the first ei to match pattern.
FirstCase[expr,pattern,default]
gives default if no element matching pattern is found.
FirstCase[expr,pattern,default,levelspec]
finds only objects that appear on levels specified by levelspec.
FirstCase
FirstCase[{e1,e2,…},pattern]
gives the first ei to match pattern, or Missing["NotFound"] if none matching pattern is found.
FirstCase[{e1,…},patternrhs]
gives the value of rhs corresponding to the first ei to match pattern.
FirstCase[expr,pattern,default]
gives default if no element matching pattern is found.
FirstCase[expr,pattern,default,levelspec]
finds only objects that appear on levels specified by levelspec.
Details and Options
- The first argument to FirstCase need not have head List and can be an association.
- FirstCase[expr,pattern:>rhs] evaluates rhs only when the pattern is found.
- In FirstCase[{e1,e2,…},pattern,default], default is only evaluated if none of the ei match.
- FirstCase[pattern][expr] is equivalent to FirstCase[expr,pattern].
- FirstCase uses standard level specifications:
-
n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2 - The default value for levelspec in FirstCase is {1}.
- 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 -1 consists of numbers, symbols, and other objects that do not have subparts.
- Level 0 corresponds to the whole expression.
- With the option setting Heads->True, FirstCase looks at heads of expressions and their parts.
- FirstCase traverses the parts of expr in a depth-first order, with leaves visited before roots.
Examples
open all close allBasic Examples (3)
Find the first element in a list matching an integer:
FirstCase[{a, b, c, 5, 6, 7}, _Integer]FirstCase[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, Except[_Integer]]Return the x from inside the first f[x_] matched:
FirstCase[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, f[x_] :> x]Pick an element from an Association:
FirstCase[<|1 -> "a", 2 -> "b", 3 -> c, 4 -> d|>, _Symbol]Scope (7)
Use the operator form of Cases:
FirstCase[_Integer]@{f[a], 2, 3, a}FirstCase[{{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}}, {_, _}]FirstCase[{{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}}, {a_, b_} -> a + b]Pick out the first nonzero element:
FirstCase[{1, 0, 2, 0, 3, 5, 6, 7, 0, 0, 0}, Except[0]]Use :> to evaluate only after explicit elements have been found:
FirstCase[{f[{}], f[{a}], g[{a, b}], f[{a, b, c}]}, g[x_] :> Length[x]]Specify a custom default value:
FirstCase[{1, 2, 3}, _ ? StringQ, "NoStrings"]Examine the elements of a matrix using a level specification:
FirstCase[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, _Symbol, Missing[], {2}]Pick elements from nested associations:
FirstCase[<|1 -> 5, 2 -> <|3 -> 1, a -> b|>|>, _Symbol, Missing[], Infinity]Options (1)
Properties & Relations (4)
The default value is only evaluated if needed:
FirstCase[{None, "A", "B"}, _String, Abort[]]FirstCase[{None, None, None}, _String, Abort[]]FirstCase returns only the first match:
FirstCase[{None, "A", "B"}, "A" | "B"]Cases returns all matches:
Cases[{None, "A", "B"}, "A" | "B"]FirstCase can be implemented with a combination of Cases and Replace:
FirstCase[{1, 2, a, Sqrt[2], Pi, E}, _Symbol]
FirstCase[{1, 2, a, Sqrt[2], Pi, E}, _StringQ]Replace[Cases[{1, 2, a, Sqrt[2], Pi, E}, _Symbol], {{} :> Missing["NotFound"], l_ :> First[l]}]
Replace[Cases[{1, 2, a, Sqrt[2], Pi, E}, _StringQ], {{} :> Missing["NotFound"], l_ :> First[l]}]FirstCase is similar to SelectFirst except that it uses a pattern instead of a function:
list = RandomInteger[{3, 7}, {10, 2}]Select the first list that has a sum of elements less than 10:
smallQ = Total[#] < 10&;FirstCase[list, x_ /; smallQ[x]]Use SelectFirst to get the same result:
SelectFirst[list, smallQ]Possible Issues (1)
A rule transforms the matching element into its right side:
FirstCase[{1, b -> Automatic, c -> 3}, _ -> Automatic]Use HoldPattern to treat the rule itself as a pattern:
FirstCase[{1, b -> Automatic, c -> 3}, HoldPattern[_ -> Automatic]]Related Guides
History
Text
Wolfram Research (2014), FirstCase, Wolfram Language function, https://reference.wolfram.com/language/ref/FirstCase.html.
CMS
Wolfram Language. 2014. "FirstCase." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FirstCase.html.
APA
Wolfram Language. (2014). FirstCase. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FirstCase.html
BibTeX
@misc{reference.wolfram_2026_firstcase, author="Wolfram Research", title="{FirstCase}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/FirstCase.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_firstcase, organization={Wolfram Research}, title={FirstCase}, year={2014}, url={https://reference.wolfram.com/language/ref/FirstCase.html}, note=[Accessed: 12-June-2026]}