LeafCount
✖
LeafCount
Details and Options

- LeafCount gives a measure of the total "size" of an expression.
- LeafCount counts the number of subexpressions in expr that correspond to "leaves" on the expression tree.
- For ordinary expressions, LeafCount is based on FullForm representation.
- For raw and other atomic expressions, the LeafCount is 1 with the following exceptions: »
-
Association based on the leaf count of the values Complex based on FullForm Rational based on FullForm - With the option setting HeadsFalse, LeafCount excludes heads of expressions and their parts.
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Options (1)Common values & functionality for each option
Applications (3)Sample problems that can be solved with this function
Find leaf count measures of the sizes of successive integrals:

https://wolfram.com/xid/0en0qxga6-v9a

Make Simplify pick expressions with minimum LeafCount:

https://wolfram.com/xid/0en0qxga6-rna


https://wolfram.com/xid/0en0qxga6-ne2

Find leaf counts in combinator expressions [more info]:

https://wolfram.com/xid/0en0qxga6-uog


https://wolfram.com/xid/0en0qxga6-h2d

Properties & Relations (6)Properties of the function, and connections to other functions
LeafCount by default includes the head and all elements inside it:

https://wolfram.com/xid/0en0qxga6-t63


https://wolfram.com/xid/0en0qxga6-ms2

For expressions with atomic heads, this corresponds to the number of vertices in TreeForm[expr]:

https://wolfram.com/xid/0en0qxga6-thr8bd

LeafCount[expr,HeadsFalse] typically gives the number of leaves in TreeForm[expr]:

https://wolfram.com/xid/0en0qxga6-mq3s4p

https://wolfram.com/xid/0en0qxga6-bah0hy


https://wolfram.com/xid/0en0qxga6-6665sz

LeafCount counts elements in rational and complex numbers just as in FullForm:

https://wolfram.com/xid/0en0qxga6-y0o


https://wolfram.com/xid/0en0qxga6-tsl

LeafCount counts the head and values of an Association:

https://wolfram.com/xid/0en0qxga6-m7ld0p

These are the five leaves in the expression:

https://wolfram.com/xid/0en0qxga6-nx68ah

Reversing the keys and values gives a different result:

https://wolfram.com/xid/0en0qxga6-lmjc8e

LeafCount treats atomic objects, such as SparseArray objects, as single expression leaves:

https://wolfram.com/xid/0en0qxga6-tgc


https://wolfram.com/xid/0en0qxga6-gpi

SparseArray objects have a complicated FullForm:

https://wolfram.com/xid/0en0qxga6-kjiehv


https://wolfram.com/xid/0en0qxga6-v3img8

For most expressions, LeafCount equals the count matching Blank[] at level {-1}:

https://wolfram.com/xid/0en0qxga6-wyy4g


https://wolfram.com/xid/0en0qxga6-jt63cw

LeafCount counts Rational and Complex numbers using their FullForm:

https://wolfram.com/xid/0en0qxga6-14u8xv


https://wolfram.com/xid/0en0qxga6-6c2vq

Count treats Rational and Complex as atoms:

https://wolfram.com/xid/0en0qxga6-649t0c


https://wolfram.com/xid/0en0qxga6-9q8uup

Possible Issues (3)Common pitfalls and unexpected behavior
LeafCount is based on the FullForm of expressions:

https://wolfram.com/xid/0en0qxga6-nk


https://wolfram.com/xid/0en0qxga6-egl


https://wolfram.com/xid/0en0qxga6-cgk

LeafCount by default includes leaves in heads:

https://wolfram.com/xid/0en0qxga6-2acen9

The head f is considered a leaf, even though it is not a leaf in a tree visualization of the expression:

https://wolfram.com/xid/0en0qxga6-lx9jvu

Use the setting HeadsFalse to only count the leaves of the tree visualization:

https://wolfram.com/xid/0en0qxga6-exb7zq

Unlike TreeForm, LeafCount takes into consideration the structure of expression heads:

https://wolfram.com/xid/0en0qxga6-fgq0bo


https://wolfram.com/xid/0en0qxga6-l16bv6

This shows the expression tree whose leaves are counted by LeafCount:

https://wolfram.com/xid/0en0qxga6-dr0hkp

Wolfram Research (1988), LeafCount, Wolfram Language function, https://reference.wolfram.com/language/ref/LeafCount.html (updated 2022).
Text
Wolfram Research (1988), LeafCount, Wolfram Language function, https://reference.wolfram.com/language/ref/LeafCount.html (updated 2022).
Wolfram Research (1988), LeafCount, Wolfram Language function, https://reference.wolfram.com/language/ref/LeafCount.html (updated 2022).
CMS
Wolfram Language. 1988. "LeafCount." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/LeafCount.html.
Wolfram Language. 1988. "LeafCount." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/LeafCount.html.
APA
Wolfram Language. (1988). LeafCount. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LeafCount.html
Wolfram Language. (1988). LeafCount. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LeafCount.html
BibTeX
@misc{reference.wolfram_2025_leafcount, author="Wolfram Research", title="{LeafCount}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/LeafCount.html}", note=[Accessed: 02-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_leafcount, organization={Wolfram Research}, title={LeafCount}, year={2022}, url={https://reference.wolfram.com/language/ref/LeafCount.html}, note=[Accessed: 02-April-2025
]}