Count
Details and Options
- The first argument to Count need not have head List.
- When used on an Association, Count tests only the values of elements, not their keys.
- Count 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 Count 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, Count looks at heads of expressions and their parts.
- Count[pattern][expr] is equivalent to Count[expr,pattern].
- Parallelize[Count[list,pattern]] computes Count[list,pattern] in parallel on all subkernels. »
Examples
open all close allBasic Examples (4)
Count how many times b occurs:
Count[{a, b, a, a, b, c, b}, b]Count powers of x in an Association:
Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, x ^ _]Count powers of x on all levels:
Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, x ^ _, Infinity]Count[<|1 -> 1 + x ^ 2, 2 -> x ^ 4, 3 -> a + (1 + x ^ 2) ^ 2|>, _Symbol, Infinity]Scope (5)
Count works with patterns:
Count[{a, 2, a, a, 1, c, b, 3, 3}, _Integer]Count the number of elements not matching b:
Count[{a, b, a, a, b, c, b, a, a}, Except[b]]Count occurrences of b down to level 2:
Count[{{a, a, b}, b, {a, b, a}}, b, 2]Count occurrences at level 2 only:
Count[{{a, a, b}, b, {a, b, a}}, b, {2}]Count all numeric expressions appearing as part of a larger expression:
Count[x ^ 3 + 1.5x ^ 2 + Pi x + 7, _ ? NumericQ, -1]A numeric level specification does not include level zero:
Count[5, _ ? NumericQ, -1]Use a two-element list to explicitly include level zero:
Count[5, _ ? NumericQ, {0, -1}]Generalizations & Extensions (1)
Options (1)
Applications (3)
Count the total number of symbols in an expression:
Count[a + b ^ 2 / (a + b), _Symbol, Infinity]Count the total number of elements greater than 0.5:
Count[RandomReal[1, {100}], u_ /; u > 0.5]Count the number of rows whose first element is 1:
Count[{{1, 2}, {2, 3}, {1, 5}}, {u_, _} /; u == 1]Properties & Relations (5)
Count returns the length of the result given by Cases:
Count[{1, "f", g, "h", "7"}, _ ? StringQ]Length[Cases[{1, "f", g, "h", "7"}, _ ? StringQ]]Count returns the length of the result given by Position:
Count[{1, "f", g, "h", "7"}, _ ? StringQ]Length[Position[{1, "f", g, "h", "7"}, _ ? StringQ]]A count at level {0} is effectively a numericized version of MatchQ:
MatchQ[5, _Integer]Count[5, _Integer, {0}] == 1For most expressions, LeafCount equals the count matching Blank[] at level {-1}:
LeafCount[1 + a + b ^ 2]Count[1 + a + b ^ 2, _, {-1}, Heads -> True]Count treats Rational and Complex as atoms:
AtomQ /@ {1 / 2, 1 + I}Count[{1 / 2, 1 + I}, _, {-1}, Heads -> True]LeafCount counts Rational and Complex numbers using their FullForm:
{1 / 2, 1 + I}//FullFormLeafCount[{1 / 2, 1 + I}]Compute Count in parallel:
Parallelize[Count[Range[10 ^ 6], _ ? PrimeQ]]Possible Issues (1)
Count looks for matches based on patterns, which may not be the same as numerical equality:
Count[{0.9, .15 * 6, 1.8 / 2}, 0.9]Write a pattern using Condition to force searching based on numerical equality:
Count[{0.9, .15 * 6, 1.8 / 2}, n_ /; n == 0.9]History
Introduced in 1988 (1.0) | Updated in 2014 (10.0)
Text
Wolfram Research (1988), Count, Wolfram Language function, https://reference.wolfram.com/language/ref/Count.html (updated 2014).
CMS
Wolfram Language. 1988. "Count." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Count.html.
APA
Wolfram Language. (1988). Count. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Count.html
BibTeX
@misc{reference.wolfram_2026_count, author="Wolfram Research", title="{Count}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Count.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_count, organization={Wolfram Research}, title={Count}, year={2014}, url={https://reference.wolfram.com/language/ref/Count.html}, note=[Accessed: 12-June-2026]}