CountsBy
Examples
open all close allBasic Examples (2)
Count the even and odd elements of a list:
CountsBy[{1, 2, 3, 2, 1, 1}, EvenQ]Count the prime and composite numbers in a list:
CountsBy[Range[1000], PrimeQ]Use the operator form of CountsBy:
CountsBy[PrimeQ][Range[1000]]Scope (1)
Properties & Relations (3)
Elements in the result appear in the order in which values of f[ei] appear in the list:
numbers = {0, 1, -1, I, -I, 1 + I, 1 - I, -1 + I, -1 + I}
CountsBy[numbers, Abs]Reverse the elements of the list, and the order of counts is reversed:
CountsBy[Reverse[numbers], Abs]Move zero to the end of the list, and yet another order of counts is generated:
CountsBy[RotateLeft[numbers], Abs]CountsBy[list,f] is effectively Counts[Map[f,list]]:
list = RandomInteger[{-1, 1}, 100];
CountsBy[list, Abs] === Counts[Map[Abs, list]]In particular, CountsBy[list,Identity] is equivalent to Counts[list]:
CountsBy[list, Identity] === Counts[list]CountsBy[expr,f] is equivalent to GroupBy[expr,f,Length]:
CountsBy[{{a, b}, {b, c}, {a, d}}, First] == GroupBy[{{a, b}, {b, c}, {a, d}}, First, Length]History
Text
Wolfram Research (2014), CountsBy, Wolfram Language function, https://reference.wolfram.com/language/ref/CountsBy.html.
CMS
Wolfram Language. 2014. "CountsBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CountsBy.html.
APA
Wolfram Language. (2014). CountsBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CountsBy.html
BibTeX
@misc{reference.wolfram_2026_countsby, author="Wolfram Research", title="{CountsBy}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/CountsBy.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_countsby, organization={Wolfram Research}, title={CountsBy}, year={2014}, url={https://reference.wolfram.com/language/ref/CountsBy.html}, note=[Accessed: 12-June-2026]}