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

ExamplesExamplesopen allclose all

Basic Examples  (6)Basic Examples  (6)

Give all parts at level -1:

In[1]:=
Click for copyable input
Out[1]=

Give all parts down to level 2:

In[1]:=
Click for copyable input
Out[1]=

Give all parts at levels 0 through infinity:

In[1]:=
Click for copyable input
Out[1]=

Get all leaves from an Association:

In[1]:=
Click for copyable input
Out[1]=

Values in an Association are at the first level:

In[1]:=
Click for copyable input
Out[1]=

Give elements from nested associations:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Introduced in 1988
(1.0)