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 generally gives the length of the maximum index, plus 1:

Verify the equality:

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:

If length-0 functions or compound heads are present, both Depth and Level must use HeadsTrue:

Successive elements from NestList have larger depths:

Possible Issues  (3)

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:

The relationship between Depth and Position can break down when compound heads are present:

Set HeadsTrue in Depth to restore the relationship:

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).

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

BibTeX

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

BibLaTeX

@online{reference.wolfram_2024_depth, organization={Wolfram Research}, title={Depth}, year={2018}, url={https://reference.wolfram.com/language/ref/Depth.html}, note=[Accessed: 14-December-2024 ]}