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)
Flatten out lists at all levels:
Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}]Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}, 1]Flatten levels 2 and 3 of an array of depth 3 into the first level of the resulting matrix:
Flatten[RandomReal[1, {3, 5, 7}], {{2, 3}, {1}}]//DimensionsScope (10)
Level Specification (6)
Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, 0]Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, 1]Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, 2]Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, 3]Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, 4]This is the same as using level
:
Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}, ∞]And the same as not specifying a level:
Flatten[{0, {1}, {{2, -2}}, {{{3}, {-3}}}, {{{{4}}}}}]u = {{a, b}, {c, d}}Flatten an array of blocks with the shape of u into a single matrix:
Flatten[{{u, 0 u}, {0 u, u}}, {{1, 3}, {2, 4}}]//MatrixFormFlatten into a single matrix, effectively using the transpose of the blocks:
Flatten[{{u, 0 u}, {0 u, u}}, {{1, 4}, {2, 3}}]//MatrixFormInput Arrays (4)
Flatten[SparseArray[{{1, 2} -> a, {6, 4} -> b}]]Normal[%]Flatten a QuantityArray object:
Flatten[QuantityArray[{{1, 2}, {3, 4}, {5, 6}}, "Knots"]]Normal[%]Flatten works with any head:
Flatten[f[f[x, y], z]]Flatten all levels with respect to g:
Flatten[f[g[u, v], f[x, y]], Infinity, g]Flatten all levels with respect to f:
Flatten[f[g[u, v], f[x, y]], Infinity, f]Applications (5)
Join lists and individual elements:
Range[5]Flatten[{%, x, %, %, y}]Table[i ^ j, {i, 3}, {j, 4}]Flatten[%]Make a flattened list of rules:
Table[i -> j, {i, 4}, {j, 3}]Flatten[%]Do a "transpose" on a ragged array:
list = Table[i + j - 1, {i, 4}, {j, i}]Flatten[list, {{2}, {1}}]Contract three levels of arrays in a single Dot operation by flattening them first:
array = RandomInteger[10, {2, 5, 6, 4}];Flatten[array, {{1}, {2, 3, 4}}].Flatten[array, {{2, 3, 4}, {1}}]Obtain the same result by explicit contraction of three pairs of levels:
TensorContract[TensorProduct[array, array], {{2, 6}, {3, 7}, {4, 8}}]Properties & Relations (5)
Flatten acts as an inverse of Partition:
Range[20]Partition[%, 4]Flatten[%]ArrayReshape acts as an inverse for Flatten on rectangular arrays:
array = RandomInteger[9, {3, 2, 4}]Flatten[array]ArrayReshape[%, {3, 2, 4}]For a rectangular array a, ArrayFlatten[a,r] is equivalent to Flatten[a,{{1,r+1},{2,r+2},…,{r,2r}}]:
array = RandomInteger[10, {2, 3, 4, 5, 6, 7, 8}];ArrayFlatten[array, 3] === Flatten[array, {{1, 4}, {2, 5}, {3, 6}}]Flatten effectively arranges elements in the lexicographic order of their indices:
Flatten[Array[a, {3, 4}]]Flatten[Array[100#1 + 10#2 + #3&, {3, 3, 3}]]For a permutation p with inverse
, Flatten[a,List/@p-1]==Transpose[a,p]:
list = Array[Subscript[a, ##]&, {2, 3, 4, 2}];p = RandomSample[Range[4]]invp = InversePermutation[p]Flatten[list, List /@ invp] == Transpose[list, p]Neat Examples (1)
Peel off successive layers of Framed:
NestList[Flatten[#, 1]&, Nest[Framed, x, 6], 10]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_2026_flatten, author="Wolfram Research", title="{Flatten}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Flatten.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_flatten, organization={Wolfram Research}, title={Flatten}, year={2007}, url={https://reference.wolfram.com/language/ref/Flatten.html}, note=[Accessed: 12-June-2026]}