Depth[expr]
gives the maximum number of indices needed to specify any part of expr, plus 1.
Depth
Depth[expr]
gives the maximum number of indices needed to specify any part of expr, plus 1.
Examples
open all close allBasic Examples (3)
Depth[a]Additional levels of nesting increase the depth:
Depth[{a}]Depth[{{a}}]Only the deepest part of the expression affects the depth:
Depth[{{{a}}}]Depth[{{{a}, b}}]Depth can work with any expression:
Depth[1 + x ^ 2]Use TreeForm to visualize depth as the number of levels of the expression:
TreeForm[1 + x ^ 2]Scope (10)
Depths of General Expressions (5)
Depth works on any expression, not just lists:
Depth[f[f[f[x]]]]Depth does not distinguish heads:
Depth[f[g[h[x]]]]Symbols and strings have depth 1:
Depth[symbol]Depth["string"]Depth[12345]Depth[3 + I]Depth[(1/2)]Numeric expressions that are not NumberQ have greater depth:
Depth[Sqrt[2]]Depth does not by default count depth in the head:
Depth[h[{{{a}}}][x, y]]Use the option setting HeadsTrue to include heads:
Depth[h[{{{a}}}][x, y], Heads -> True]Depths of Special Expressions (5)
Keys are ignored when computing the depth of an association:
Depth[<|1 -> a|>]Depth[<|f[g[x]] -> a|>]Depth[{a}]The depth of values is considered when computing the depth of an association:
Depth[<|1 -> x|>]Depth[<|1 -> x + y|>]Depth of a nested association:
Depth[<|1 -> <|2 -> 3|>|>]Depth treats SparseArray and structured array objects just like the corresponding ordinary lists:
Depth[SparseArray[Automatic, {1, 1, 1, 1}, 0, {1, {{0, 1}, {{1, 1, 1}}}, {x}}]]Depth[SymmetrizedArray[StructuredArray`StructuredData[{4, 4}, {{{1, 2} -> a, {2, 3} -> b},
Symmetric[{1, 2}]}]]]Most raw objects have a depth of 1:
Depth[Graph[{1 -> 2, 3 -> 4}]]Depth[MeshRegion[{{0}, {1}, {2}, {3}}, {Line[{1, 2}], Line[{3, 4}]}]]Options (1)
Applications (2)
Find how deeply nested results from integrals are:
Table[Depth[Integrate[1 / (x ^ n - 1), x]], {n, 10}]Find the depths of combinator expressions [more info]:
combs = NestList[# /. s[x_][y_][z_] -> x[z][y[z]]&, s[s][s][s[s]][s][s], 4]Depth /@ combsConsidering the heads increases the depth:
Depth[#, Heads -> True]& /@ combsIncrease the nesting level of the combinator expressions:
moreCombs = NestList[# /. s[x_][y_][z_] -> x[z][y[z]]&, s[s][s][s[s]][s][s], 30];Depth /@ moreCombsDepth[#, Heads -> True]& /@ moreCombsProperties & Relations (5)
Depth generally gives the length of the maximum index, plus 1:
Position[(1 + x)(2 + y ^ 3), _]Depth[(1 + x)(2 + y ^ 3)]% == Max[Length /@ %%] + 1Depth considers the deepest part of an expression:
{Depth[{{a}, {b}}], Depth[{{a}, {f[b]}}]}ArrayDepth only considers the levels to which the expression is completely rectangular:
{ArrayDepth[{{a}, {b}}], ArrayDepth[{{a}, {f[b]}}]}For completely rectangular expressions, Depth gives a result one greater than ArrayDepth:
{Depth[{a, b}], ArrayDepth[{a, b}]}{Depth[{{a}, {b}}], ArrayDepth[{{a}, {b}}]}Depth[expr] is the smallest positive level k for which Level[expr,{k}] returns an empty list:
expr = a + f[x, y ^ n];Table[Level[expr, {k}], {k, 0, Depth[expr]}]If length-0 functions or compound heads are present, both Depth and Level must use HeadsTrue:
expr = a + f[];Table[Level[expr, {k}], {k, 0, Depth[expr]}]Table[Level[expr, {k}, Heads -> True], {k, 0, Depth[expr, Heads -> True]}]Successive elements from NestList have larger depths:
NestList[f, x, 5]Depth /@ %Possible Issues (3)
Depth returns a depth one greater than that returned by ArrayDepth:
{Depth[1], ArrayDepth[1]}{Depth[{1, 2, 3}], ArrayDepth[{1, 2, 3}]}Depth counts an association as a single level:
Depth[<|a -> x, b -> y|>]It counts the corresponding list of rules as two levels:
Depth[{a -> x, b -> y}]The relationship between Depth and Position can break down when compound heads are present:
Depth[f[][]] == 1 + Max[Length /@ Position[f[][], _]]Set HeadsTrue in Depth to restore the relationship:
Depth[f[][], Heads -> True] == 1 + Max[Length /@ Position[f[][], _]]See Also
Tech Notes
Related Guides
Related Links
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2010 (8.0) ▪ 2018 (11.3)
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_2026_depth, author="Wolfram Research", title="{Depth}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/Depth.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_depth, organization={Wolfram Research}, title={Depth}, year={2018}, url={https://reference.wolfram.com/language/ref/Depth.html}, note=[Accessed: 12-June-2026]}