Depth

Depth[expr]

gives the maximum number of indices needed to specify any part of expr, plus 1.

Details and Options

  • Raw objects have depth 1.
  • With the option setting Heads->True, Depth includes heads of expressions and their parts.

Examples

open allclose all

Basic Examples  (3)

Symbols have depth 1:

Additional levels of nesting increase the depth:

Only the deepest part of the expression affects the depth:

Depth can work with any expression:

Use TreeForm to visualize depth as the number of levels of the expression:

Scope  (10)

Depths of General Expressions  (5)

Depth works on any expression, not just lists:

Depth does not distinguish heads:

Symbols and strings have depth 1:

All numbers have depth 1:

Numeric expressions that are not NumberQ have greater depth:

Depth does not by default count depth in the head:

Use the option setting HeadsTrue to include heads:

Depths of Special Expressions  (5)

Keys are ignored when computing the depth of an association:

Compare with:

The depth of values is considered when computing the depth of an association:

Depth of a nested association:

Depth treats SparseArray and structured array objects just like the corresponding ordinary lists:

Most raw objects have a depth of 1:

Options  (1)

Heads  (1)

By default, the depth of heads is ignored:

Include the depth of heads:

Applications  (2)

Find how deeply nested results from integrals are:

Find the depths of combinator expressions [more info]:

Considering the heads increases the depth:

Increase the nesting level of the combinator expressions:

Properties & Relations  (5)

Depth gives the length of the maximum index, plus 1:

Depth considers the deepest part of an expression:

ArrayDepth only considers the levels to which the expression is completely rectangular:

For completely rectangular expressions, Depth gives a result one greater than ArrayDepth:

Depth[expr] is the smallest positive level k for which Level[expr,{k}] returns an empty list:

Successive elements from NestList have larger depths:

Possible Issues  (2)

Depth returns a depth one greater than that returned by ArrayDepth:

Depth counts an association as a single level:

It counts the corresponding list of rules as two levels:

Wolfram Research (1988), Depth, Wolfram Language function, https://reference.wolfram.com/language/ref/Depth.html (updated 2018).

Text

Wolfram Research (1988), Depth, Wolfram Language function, https://reference.wolfram.com/language/ref/Depth.html (updated 2018).

BibTeX

@misc{reference.wolfram_2021_depth, author="Wolfram Research", title="{Depth}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/Depth.html}", note=[Accessed: 21-September-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_depth, organization={Wolfram Research}, title={Depth}, year={2018}, url={https://reference.wolfram.com/language/ref/Depth.html}, note=[Accessed: 21-September-2021 ]}

CMS

Wolfram Language. 1988. "Depth." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2018. https://reference.wolfram.com/language/ref/Depth.html.

APA

Wolfram Language. (1988). Depth. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Depth.html