Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > List Manipulation > Rearranging & Restructuring Lists >
Mathematica > Data Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists >

Flatten

Flatten[list]
flattens out nested lists.
Flatten[list, n]
flattens to level n.
Flatten[list, n, h]
flattens subexpressions with head h.
Flatten[list, {{s11, s12, ...}, {s21, s22, ...}, ...}]
flattens list by combining all levels sij to make each level i in the result.
  • 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.
  • If the mij are matrices, Flatten[{{m11, m12}, {m21, m22}}, {{1, 3}, {2, 4}}] effectively constructs a single matrix from the "blocks" mij.
  • Flatten[list, {{i1}, {i2}, ...}] effectively transposes levels in list, putting level ik in list at level k in the result. Note that the function Transpose in effect uses an inverse of this specification.
Flatten out lists at all levels:
Flatten only at level 1:
Flatten out lists at all levels:
In[1]:=
Click for copyable input
Out[1]=
 
Flatten only at level 1:
In[1]:=
Click for copyable input
Out[1]=
No flattening:
Flatten to level 1:
Flatten to level 2:
Flatten to level 3:
Flatten to level 4:
This is the same as using level Infinity:
And the same as not specifying a level:
Flatten a sparse array:
Flatten works with any head:
Flatten all levels with respect to g:
Flatten all levels with respect to f:
Here is a matrix:
Flatten an array of blocks with the shape of u into a single matrix.
Flatten into a single matrix effectively using the transpose of the blocks:
Join lists and individual elements:
Unravel a matrix:
Make a flattened list of rules:
Do a "transpose" on a ragged array:
Flatten acts as an inverse of Partition:
Flatten effectively arranges elements in the lexicographic order of their indices:
For a permutation p with inverse p^(-1), Flatten[a, List/@p-1]EqualTranspose[a, p]:
A random permutation:
Get its inverse:
Peel off successive layers of Framed:
New in 1 | Last modified in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team