This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Lists and Matrices / Structure Manipulation  /
Flatten

  • Flatten[ list ] flattens out nested lists.
  • Flatten[ list , n ] flattens to level n.
  • Flatten[ list , n , h ] flattens subexpressions with head h.
  • Example: Flatten[ a, b,c , d ].
  • Flatten "unravels" lists, effectively just deleting inner braces.
  • Flatten[ list , n ] effectively flattens the top level in list n times.
  • Flatten[ f [ e , ... ]] flattens out subexpressions with head f.
  • See the Mathematica book: Section 1.8.12Section 2.2.10.
  • See also: Partition.

    Further Examples

    The second command only flattens out sublists at the first level.

    In[1]:=

    Out[1]=

    In[2]:=

    Out[2]=

    This removes nested occurrences of the function g.

    In[3]:=

    Out[3]=

    In[4]:=

    Out[4]=

    You can use Flatten to splice sequences of elements into lists or other expressions.

    In[5]:=

    Out[5]=