# Nested Lists

 {list1,list2,…} list of lists Table[expr,{i,m},{j,n},…] m×n×… table of values of expr Array[f,{m,n,…}] m×n×… array of values f[i,j,…] Normal[SparseArray[{{i1,j1,…}->v1,…},{m,n,…}]] m×n×… array with element {is,js,…} being vs Outer[f,list1,list2,…] generalized outer product with elements combined using f Tuples[list,{m,n,…}] all possible m×n×… arrays of elements from list

Ways to construct nested lists.

This generates a table corresponding to a 2×3 nested list:
 In:= Out= This generates an array corresponding to the same nested list:
 In:= Out= Elements not explicitly specified in the sparse array are taken to be 0:
 In:= Out= Each element in the final list contains one element from each input list:
 In:= Out= Functions like Array, SparseArray, and Outer always generate full arrays, in which all sublists at a particular level are the same length.

 Dimensions[list] the dimensions of a full array ArrayQ[list] test whether all sublists at a given level are the same length ArrayDepth[list] the depth to which all sublists are the same length

Functions for full arrays.

The Wolfram Language can handle arbitrary nested lists. There is no need for the lists to form a full array. You can easily generate ragged arrays using Table.

This generates a triangular array:
 In:= Out= Flatten[list] flatten out all levels of list Flatten[list,n] flatten out the top n levels ArrayFlatten[list,rank] create a flattened array from an array of arrays

Flattening out sublists and subarrays.

This generates a 2×3 array:
 In:= Out= Flatten in effect puts elements in lexicographic order of their indices:
 In:= Out= This creates a matrix from a block matrix:
 In:= Out= Transpose[list] transpose the top two levels of list Transpose[list,{n1,n2,…}] put the k level in list at level nk

Transposing levels in nested lists.

This generates a 2×2×2 array:
 In:= Out= This permutes levels so that level 3 appears at level 1:
 In:= Out= This restores the original array:
 In:= Out= Map[f,list,{n}] map f across elements at level n Apply[f,list,{n}] apply f to the elements at level n MapIndexed[f,list,{n}] map f onto parts at level n and their indices

Applying functions in nested lists.

Here is a nested list:
 In:= This maps a function f at level 2:
 In:= Out= This applies the function at level 2:
 In:= Out= This applies f to both parts and their indices:
 In:= Out= Partition[list,{n1,n2,…}] partition into n1×n1×… blocks PadLeft[list,{n1,n2,…}] pad on the left to make an n1×n1×… array PadRight[list,{n1,n2,…}] pad on the right to make an n1×n1×… array RotateLeft[list,{n1,n2,…}] rotate nk places to the left at level k RotateRight[list,{n1,n2,…}] rotate nk places to the right at level k

Operations on nested lists.

Here is a nested list:
 In:= This rotates different amounts at each level:
 In:= Out= This pads with zeros to make a 2×3×3 array:
 In:= Out= 