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
Normal[SparseArray[{{i1,j1,}->v1,},{m,n,}]]
m×n× array with element being
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[1]:=
Click for copyable input
Out[1]=
This generates an array corresponding to the same nested list.
In[2]:=
Click for copyable input
Out[2]=
Elements not explicitly specified in the sparse array are taken to be .
In[3]:=
Click for copyable input
Out[3]=
Each element in the final list contains one element from each input list.
In[4]:=
Click for copyable input
Out[4]=

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[5]:=
Click for copyable input
Out[5]=
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[6]:=
Click for copyable input
Out[6]=
Flatten in effect puts elements in lexicographic order of their indices.
In[7]:=
Click for copyable input
Out[7]=
This creates a matrix from a block matrix.
In[8]:=
Click for copyable input
Out[8]=
Transpose[list]transpose the top two levels of list
Transpose[list,{n1,n2,}]put the k^(th) level in list at level

Transposing levels in nested lists.

This generates a 2×2×2 array.
In[9]:=
Click for copyable input
Out[9]=
This permutes levels so that level 3 appears at level 1.
In[10]:=
Click for copyable input
Out[10]=
This restores the original array.
In[11]:=
Click for copyable input
Out[11]=
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[12]:=
Click for copyable input
This maps a function at level 2.
In[13]:=
Click for copyable input
Out[13]=
This applies the function at level 2.
In[14]:=
Click for copyable input
Out[14]=
This applies to both parts and their indices.
In[15]:=
Click for copyable input
Out[15]=
Partition[list,{n1,n2,}]partition into ×× blocks
PadLeft[list,{n1,n2,}]pad on the left to make an ×× array
PadRight[list,{n1,n2,}]pad on the right to make an ×× array
RotateLeft[list,{n1,n2,}]rotate places to the left at level k
RotateRight[list,{n1,n2,}]rotate places to the right at level k

Operations on nested lists.

Here is a nested list.
In[16]:=
Click for copyable input
This rotates different amounts at each level.
In[17]:=
Click for copyable input
Out[17]=
This pads with zeros to make a 2×3×3 array.
In[18]:=
Click for copyable input
Out[18]=