

Flatten
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 all close allBasic Examples (3)
Scope (10)
Level Specification (6)
Input Arrays (4)
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:
See Also
Partition ArrayFlatten FlattenAt Transpose ArrayReshape ArrayReduce TakeDrop
Function Repository: AssociationKeyFlatten FlatTable
Tech Notes
Related Links
- An Elementary Introduction to the Wolfram Language : Graphs and Networks
- An Elementary Introduction to the Wolfram Language : More about Numbers
- An Elementary Introduction to the Wolfram Language : Rearranging Lists
- An Elementary Introduction to the Wolfram Language : Parts of Lists
- NKS|Online (A New Kind of Science)
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2007 (6.0)
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
BibTeX
@misc{reference.wolfram_2025_flatten, author="Wolfram Research", title="{Flatten}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Flatten.html}", note=[Accessed: 13-August-2025]}
BibLaTeX
@online{reference.wolfram_2025_flatten, organization={Wolfram Research}, title={Flatten}, year={2007}, url={https://reference.wolfram.com/language/ref/Flatten.html}, note=[Accessed: 13-August-2025]}