Cases[{e1,e2,…},pattern]
gives a list of the ei that match the pattern.
Cases[{e1,…},patternrhs]
gives a list of the values of rhs corresponding to the ei that match the pattern.
Cases[expr,pattern,levelspec]
gives a list of all parts of expr on levels specified by levelspec that match the pattern.
Cases[expr,patternrhs,levelspec]
gives the values of rhs that match the pattern.
Cases[expr,pattern,levelspec,n]
gives the first n parts in expr that match the pattern.
Cases
Cases[{e1,e2,…},pattern]
gives a list of the ei that match the pattern.
Cases[{e1,…},patternrhs]
gives a list of the values of rhs corresponding to the ei that match the pattern.
Cases[expr,pattern,levelspec]
gives a list of all parts of expr on levels specified by levelspec that match the pattern.
Cases[expr,patternrhs,levelspec]
gives the values of rhs that match the pattern.
Cases[expr,pattern,levelspec,n]
gives the first n parts in expr that match the pattern.
Details and Options
- The first argument to Cases need not have head List.
- When used on an Association, Cases picks out elements according to their values.
- Cases[expr,pattern:>rhs] evaluates rhs only when the pattern is found.
- Cases[pattern][list] is equivalent to Cases[list,pattern].
- Cases 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 Cases 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, Cases looks at heads of expressions, and their parts.
- Cases traverses the parts of expr in a depth-first order, with leaves visited before roots.
- Parallelize[Cases[expr,pattern]] or ParallelCases[expr,pattern] computes Cases[expr,pattern] in parallel on all subkernels. »
Examples
open all close allBasic Examples (4)
Find cases that explicitly match integers:
Cases[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, _Integer]Find cases that do not match integers:
Cases[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, Except[_Integer]]Return the x from inside each f[x_] matched:
Cases[{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}, f[x_] -> x]Use the operator form of Cases:
Cases[_Integer][{1, 1, f[a], 2, 3, y, f[8], 9, f[10]}]Pick elements from an Association:
Cases[<|1 -> "a", 2 -> "b", 3 -> c, 4 -> d|>, _Symbol]Scope (10)
Find all cases of lists of two elements:
Cases[{{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}}, {_, _}]Find the sum of every pair of elements:
Cases[{{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}}, {a_, b_} -> a + b]Pick out elements that are not 0:
Cases[{1, 0, 2, 0, 3, 5, 6, 7, 0, 0, 0}, Except[0]]Pick out cases of integers down to level 2:
Cases[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, _Integer, 2]Return only the first three matching elements:
Cases[Sqrt[Range[100]], _Integer, {1}, 3]Use :> to evaluate only after explicit elements have been found:
Cases[{f[{a, b}], f[{a}], g[{a}], f[{a, b, c, d}]}, f[x_] :> Length[x]]Find all possible subexpressions at any level:
Cases[{1, 2, f[a, b]}, _, {0, Infinity}]Find all possible subexpressions at any level, including the head of the expression:
Cases[{1, 2, f[a, b]}, _, {0, Infinity}, Heads -> True]Pick elements from nested associations:
Cases[<|1 -> c, 2 -> <|3 -> 1, a -> b|>|>, _Symbol, Infinity]Pick elements from a List matching an Association:
Cases[{<|a -> 1|>, <|b -> 2|>}, <|a -> _|>]Find keys of each matching Association:
Cases[{<|a -> 1|>, <|b -> 2|>, <|c -> 1|>}, <|x_ -> 1|> -> x]Pick pairs of keys from each association in a list:
Cases[{<|x -> 1, z -> 1|>, <|x -> 1, y -> 2|>}, <|a_ -> _, b_ -> _|> :> {a, b}]Find associations with the key satisfying a condition:
Cases[{<|1 -> x|>, <|2 -> x|>}, <|a_ /; EvenQ[a] -> _|>]Cases[{<|1 -> x|>, <|2 -> x|>}, <|a_ -> _|> /; EvenQ[a]]Options (1)
Properties & Relations (2)
Possible Issues (1)
A rule transforms the matching elements into its right side:
Cases[{1, b -> Automatic, c -> 3}, _ -> Automatic]Use HoldPattern to treat the rule itself as a pattern:
Cases[{1, b -> Automatic, c -> 3}, HoldPattern[_ -> Automatic]]See Also
FirstCase Select BinLists GatherBy Position MemberQ Count SequenceCases StringCases ReplaceList Collect DeleteCases Except ParallelCases
Function Repository: UniqueCases PositionedCases MapCases HighlightCases GroupCases
Tech Notes
Related Workflows
- Import Tabular Data from a Notebook
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2014 (10.0)
Text
Wolfram Research (1988), Cases, Wolfram Language function, https://reference.wolfram.com/language/ref/Cases.html (updated 2014).
CMS
Wolfram Language. 1988. "Cases." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Cases.html.
APA
Wolfram Language. (1988). Cases. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Cases.html
BibTeX
@misc{reference.wolfram_2026_cases, author="Wolfram Research", title="{Cases}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Cases.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_cases, organization={Wolfram Research}, title={Cases}, year={2014}, url={https://reference.wolfram.com/language/ref/Cases.html}, note=[Accessed: 12-June-2026]}