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 allBasic Examples (3)
Scope (5)
Generalizations & Extensions (4)
Flatten works with any head:
Flatten all levels with respect to g:
Flatten all levels with respect to f:
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:
Applications (5)
Join lists and individual elements:
Make a flattened list of rules:
Do a "transpose" on a ragged array:
Contract three levels of arrays in a single Dot operation by flattening them first:
Obtain the same result by explicit contraction of three pairs of levels:
Properties & Relations (5)
Flatten acts as an inverse of Partition:
ArrayReshape acts as an inverse for Flatten on rectangular arrays:
For a rectangular array a, ArrayFlatten[a,r] is equivalent to Flatten[a,{{1,r+1},{2,r+2},…,{r,2r}}]:
Flatten effectively arranges elements in the lexicographic order of their indices:
For a permutation p with inverse , Flatten[a,List/@p-1]==Transpose[a,p]:
Neat Examples (1)
Peel off successive layers of Framed:
Text
Wolfram Research (1988), Flatten, Wolfram Language function, https://reference.wolfram.com/language/ref/Flatten.html (updated 2007).
CMS
Wolfram Language. 1988. "Flatten." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2007. https://reference.wolfram.com/language/ref/Flatten.html.
APA
Wolfram Language. (1988). Flatten. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Flatten.html