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.

Details

  • 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 flattens out levels in SparseArray objects just as in the corresponding ordinary arrays. »

Examples

open allclose all

Basic Examples  (3)

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]=

Flatten levels 2 and 3 of an array of depth 3 into the first level of the resulting matrix:

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

Scope  (5)

Generalizations & Extensions  (4)

Applications  (5)

Properties & Relations  (5)

Neat Examples  (1)

See Also

Partition  ArrayFlatten  FlattenAt  Transpose  ArrayReshape  TakeDrop

Tutorials

Introduced in 1988
(1.0)
| Updated in 2007
(6.0)