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:

Flatten only at level 1:

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

Scope  (5)

Level Specification  (5)

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 :

And the same as not specifying a level:

Generalizations & Extensions  (4)

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:

Applications  (5)

Join lists and individual elements:

Unravel a matrix:

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

A random permutation:

Get its inverse:

Neat Examples  (1)

Peel off successive layers of Framed:

Wolfram Research (1988), Flatten, Wolfram Language function, https://reference.wolfram.com/language/ref/Flatten.html (updated 2007).

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_2024_flatten, author="Wolfram Research", title="{Flatten}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Flatten.html}", note=[Accessed: 22-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_flatten, organization={Wolfram Research}, title={Flatten}, year={2007}, url={https://reference.wolfram.com/language/ref/Flatten.html}, note=[Accessed: 22-November-2024 ]}