This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Practical Introduction / Lists  /

1.8.12 Advanced Topic: Rearranging Nested Lists

You will encounter nested lists if you use matrices or generate multidimensional arrays and tables. Rearranging nested lists can be a complicated affair, and you will often have to experiment to get the right combination of commands.


Functions for rearranging nested lists.




  • Here is a


    array.
  • In[1]:= t = {{a, b}, {c, d}, {e, f}}

    Out[1]=




  • You can rearrange it to get a


    array.
  • In[2]:= Transpose[t]

    Out[2]=

  • This "flattens out" sublists. You can think of it as effectively just removing the inner sets of braces.
  • In[3]:= Flatten[t]

    Out[3]=




  • Here is a


    array.
  • In[4]:= t = Table[i^2 +j^2 +k^2, {i, 2}, {j, 2}, {k, 2}]

    Out[4]=

  • This flattens out all the levels.
  • In[5]:= Flatten[t]

    Out[5]=

  • This flattens only the first level of sublists.
  • In[6]:= Flatten[t, 1]

    Out[6]=

  • This flattens out only the sublist that appears at position 2.
  • In[7]:= FlattenAt[{{a, b}, {c, d}}, 2]

    Out[7]=

    There are many other operations you can perform on nested lists. We will discuss some more of them when we look at Map, Apply, Scan and Level in Part 2.