Level

Level[expr,levelspec]

gives a list of all subexpressions of expr on levels specified by levelspec.

Level[expr,levelspec,f]

applies f to the sequence of subexpressions.

Details and Options

  • Level uses standard level specifications:
  • nlevels 1 through
    Infinitylevels 1 through Infinity
    {n}level only
    {n1,n2}levels through
  • Level[expr,{-1}] gives a list of all "atomic" objects in expr.
  • A positive level consists of all parts of expr specified by indices.
  • A negative level consists of all parts of expr with depth .
  • Level 0 corresponds to the whole expression.
  • With the option setting Heads->True, Level includes heads of expressions and their parts.
  • Level traverses expressions in depthfirst order, so that the subexpressions in the final list are ordered lexicographically by their indices.

Examples

open allclose all

Basic Examples  (6)

Give all parts at level -1:

Give all parts down to level 2:

Give all parts at levels 0 through infinity:

Get all leaves from an Association:

Values in an Association are at the first level:

Give elements from nested associations:

Generalizations & Extensions  (5)

Down successive numbers of levels:

Negative levels:

Ranges of levels:

Use different heads at each level:

Including heads:

Applications  (1)

Find the leaf elements in an expression:

Include heads:

Find unique leaf expressions:

Properties & Relations  (2)

Level always lists parts in the lexicographic order of their indices:

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

Possible Issues  (1)

Level by default starts at level 1, so it does not include the whole expression:

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

Text

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

CMS

Wolfram Language. 1988. "Level." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Level.html.

APA

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

BibTeX

@misc{reference.wolfram_2024_level, author="Wolfram Research", title="{Level}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Level.html}", note=[Accessed: 09-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_level, organization={Wolfram Research}, title={Level}, year={1988}, url={https://reference.wolfram.com/language/ref/Level.html}, note=[Accessed: 09-November-2024 ]}