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:

Introduced in 1988
 (1.0)
 |
Updated in 2003
 (5.0)
2010
 (8.0)
2018
 (11.3)