1.8.12 Advanced Topic: Rearranging Nested ListsYou will encounter nested lists if you use matrices or generate multidimensional arrays and tables. Mathematica provides many functions for handling such lists.
| Flatten[list] | flatten out all levels in list | | Flatten[list, n] | flatten out the top n levels in list | Partition[list, { , , ... }] | partition into blocks of size | | Transpose[list] | interchange the top two levels of lists | RotateLeft[list, { , , ... }] | rotate successive levels by places | PadLeft[list, { , , ... }] | pad successive levels to be length |
A few functions for rearranging nested lists. | This "flattens out" sublists. You can think of it as effectively just removing all inner braces. | |
In[1]:=
Flatten[{{a}, {b, {c}}, {d}}]
|
Out[1]=
|
|
| This flattens out only one level of sublists. | |
In[2]:=
Flatten[{{a}, {b, {c}}, {d}}, 1]
|
Out[2]=
|
|
There are many other operations you can perform on nested lists. We will discuss more of them in Section 2.4.
|