({{, (RefLink[Level, paclet:ref/Level])[expr,levelspec]
gives a list of all subexpressions of expr on levels specified by levelspec. }, {, (RefLink[Level, paclet:ref/Level])[expr,levelspec,f]
applies f to the sequence of subexpressions. }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[Level, paclet:ref/Level]) uses standard level specifications:

(RefLink[Level, paclet:ref/Level])[expr,{-1}] gives a list of all "atomic" objects in expr.

A positive level n consists of all parts of expr specified by n indices.

A negative level -n consists of all parts of expr with depth n.

Level 0 corresponds to the whole expression.

With the option setting (RefLink[Heads, paclet:ref/Heads])->(RefLink[True, paclet:ref/True]), (RefLink[Level, paclet:ref/Level]) includes heads of expressions and their parts.

(RefLink[Level, paclet:ref/Level]) traverses expressions in depth-first order, so that the subexpressions in the final list are ordered lexicographically by their indices.

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (6)

Give all parts at level -1:

Level[a+f[x,y^n],{-1}]

Give all parts down to level 2:

Level[a+f[x,y^n],2]

Give all parts at levels 0 through infinity:

Level[a+f[x,y^n],{0,Infinity}]

Get all leaves from an (RefLink[Association, paclet:ref/Association]):

Level[<|1->a,2->b|>,-1]

Values in an (RefLink[Association, paclet:ref/Association]) are at the first level:

Level[<|1->a,2->b|>,{1}]

Give elements from nested associations:

Level[<|1-><|a->b,c->d|>|>,{2}]

Level[<|1-><|a->b,c->d|>|>,{1,2}]

OpenersExampleSection1(Spacer1)Generalizations & Extensions  (5)

Down successive numbers of levels:

Level[{{{{a}}}},1]

Level[{{{{a}}}},2]

Level[{{{{a}}}},3]

Level[{{{{a}}}},4]

Level[{{{{a}}}},5]

Negative levels:

Level[{{{{a}}}},-1]

Level[{{{{a}}}},-2]

Level[{{{{a}}}},-3]

Level[{{{{a}}}},-4]

Level[{{{{a}}}},-5]

Ranges of levels:

Level[{{{{a}}}},{2,3}]

Level[{{{{a}}}},{0,-1}]

Use different heads at each level:

Level[h0[h1[h2[h3[a]]]],{0,-1}]

Including heads:

Level[{{{{a}}}},3,Heads->True]

Level[x^2+y^3,3,Heads->True]

OpenersExampleSection3(Spacer1)Applications  (1)

Find the leaf elements in an expression:

Integrate[Sin[Tan[x]],x]

Level[%,{-1}]

Include heads:

Level[Integrate[Sin[Tan[x]],x],{-1},Heads->True]

Find unique leaf expressions:

Union[Level[Integrate[Sin[Tan[x]],x],{-1},Heads->True]]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[Level, paclet:ref/Level]) always lists parts in the lexicographic order of their indices:

Array[a,{2,2,2}]

Level[%,{3}]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

(RefLink[Level, paclet:ref/Level]) by default starts at level 1, so it does not include the whole expression:

Level[h1[h2[h3[x]]],-1]

Level[h1[h2[h3[x]]],{0,-1}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Apply, paclet:ref/Apply])  ▪  (RefLink[Map, paclet:ref/Map])  ▪  (RefLink[Scan, paclet:ref/Scan])  ▪  (RefLink[Depth, paclet:ref/Depth])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Expressions as Trees, paclet:tutorial/ExpressionsAsTrees])}, {▪(RefLinkPlain[Levels in Expressions, paclet:tutorial/LevelsInExpressions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Expression Structure, paclet:guide/ExpressionStructure])}})}})