WOLFRAM

LeafCount[expr]

gives the total number of indivisible subexpressions in expr.

Details and Options

Examples

open allclose all

Basic Examples  (1)Summary of the most common use cases

Find the number of "leaves" in an expression tree:

Out[1]=1

These are the leaves in the expression:

Out[2]=2

Options  (1)Common values & functionality for each option

Heads  (1)

By default, leaves in heads are included:

Out[1]=1

Exclude leaves in heads:

Out[2]=2

Applications  (3)Sample problems that can be solved with this function

Find leaf count measures of the sizes of successive integrals:

Out[1]=1

Make Simplify pick expressions with minimum LeafCount:

Out[1]=1
Out[2]=2

Find leaf counts in combinator expressions [more info]:

Out[1]=1
Out[2]=2

Properties & Relations  (6)Properties of the function, and connections to other functions

LeafCount by default includes the head and all elements inside it:

Out[3]=3
Out[4]=4

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

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

Out[3]=3

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

Out[1]=1

LeafCount counts the head and values of an Association:

Out[1]=1

These are the five leaves in the expression:

Out[2]=2

Reversing the keys and values gives a different result:

Out[3]=3

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

Out[1]=1
Out[2]=2

SparseArray objects have a complicated FullForm:

But they are atomic:

Out[4]=4

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

Out[1]=1
Out[2]=2

LeafCount counts Rational and Complex numbers using their FullForm:

Out[4]=4

Count treats Rational and Complex as atoms:

Out[5]=5
Out[6]=6

Possible Issues  (3)Common pitfalls and unexpected behavior

LeafCount is based on the FullForm of expressions:

Out[83]=83
Out[82]=82

LeafCount by default includes leaves in heads:

Out[1]=1

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

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

Out[3]=3

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

Out[2]=2

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

Out[3]=3
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).

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 ]}

@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 ]}

@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 ]}