Explore the latest version of An Elementary Introduction to the Wolfram Language »
30Rearranging Lists
Its common for lists that come out of one computation to have to be rearranged before going into another computation. For example, one might have a list of pairs, and need to convert it into a pair of lists, or vice versa.
In[1]:=
Click for copyable input
Out[1]=
Transpose the pair of lists back to a list of pairs:
In[2]:=
Click for copyable input
Out[2]=
Thread is a closely related operation, often useful for generating input to Graph.
Thread across the elements of two lists:
In[3]:=
Click for copyable input
Out[3]=
Partition takes a list, and partitions it into blocks of a specified size.
Partition a 12-element list into blocks of size 3:
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
If you dont tell it otherwise, Partition breaks a list up into non-overlapping blocks. But you can also tell it to break the list into blocks that have some specified offset.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
Use an offset of 2 instead:
In[8]:=
Click for copyable input
Out[8]=
Partition takes a list and breaks it into sublists. Flatten flattens out sublists.
Make a list of lists from digits of successive integers:
In[9]:=
Click for copyable input
Out[9]=
Make a flattened version:
In[10]:=
Click for copyable input
Out[10]=
Make a plot from the sequence of digits:
In[11]:=
Click for copyable input
Out[11]=
Flatten will normally flatten out all levels of lists. But quite often you only want to flatten, say, one level of lists. This makes a 4×4 table in which each element is itself a list.
Make a list of lists of lists:
In[12]:=
Click for copyable input
Out[12]=
In[13]:=
Click for copyable input
Out[13]=
Flatten out only one level of list:
In[14]:=
Click for copyable input
Out[14]=
ArrayFlatten is a generalization of Flatten, which takes arrays of arrays, and flattens them into individual arrays.
This generates a deeply nested structure thats hard to understand:
In[15]:=
Click for copyable input
Out[15]=
ArrayFlatten makes a structure thats a little easier to understand:
In[16]:=
Click for copyable input
Out[16]=
With ArrayPlot, its considerably easier to see whats going on:
In[17]:=
Click for copyable input
Out[17]=
In[18]:=
Click for copyable input
Out[18]=
There are many other ways to rearrange lists. For example, Split splits a list into runs of identical elements.
Split a list into sequences of successive identical elements:
In[19]:=
Click for copyable input
Out[19]=
Gather, on the other hand, gathers identical elements together, wherever they appear.
Gather identical elements together in lists:
In[20]:=
Click for copyable input
Out[20]=
GatherBy gathers elements according to the result of applying a function to them. Here its using LetterQ, so that it gathers separately letters and non-letters.
Gather characters according to whether they are letters or not:
In[21]:=
Click for copyable input
Out[21]=
SortBy sorts according to the result of applying a function.
Sort normally sorts shorter lists before longer ones:
In[22]:=
Click for copyable input
Out[22]=
Here SortBy is told to sort by the first element in each list:
In[23]:=
Click for copyable input
Out[23]=
Sort sorts a list in order. Union also removes any repeated elements.
Find all the distinct elements that appear:
In[24]:=
Click for copyable input
Out[24]=
You can use Union to find the union of elements that appear in any of several lists.
Get a list of all elements that appear in any of the lists:
In[25]:=
Click for copyable input
Out[25]=
In[26]:=
Click for copyable input
Out[26]=
In[27]:=
Click for copyable input
Out[27]=
In[28]:=
Click for copyable input
Out[28]=
Letters that appear in Swedish but not English:
In[29]:=
Click for copyable input
Out[29]=
Another of the many functions you can apply to lists is Riffle, which inserts things between successive elements of a list.
Riffle x in between the elements of a list:
In[30]:=
Click for copyable input
Out[30]=
Riffle -- into a list of characters:
In[31]:=
Click for copyable input
Out[31]=
Join everything together in a single string:
In[32]:=
Click for copyable input
Out[32]=
Functions like Partition let you take a list and break it into sublists. Sometimes youll instead just want to start from a collection of possible elements, and form lists from them.
Permutations gives all possible orderings, or permutations, of a list.
Generate a list of the 3!=3×2×1=6 possible orderings of 3 elements:
In[33]:=
Click for copyable input
Out[33]=
In[34]:=
Click for copyable input
Out[34]=
Tuples takes a list of elements, and generates all possible combinations of a given number of those elements.
Generate a list of all possible triples of red and green:
In[35]:=
Click for copyable input
Out[35]=
RandomChoice lets you make a random choice from a list of elements.
Make a single random choice from a list:
In[36]:=
Click for copyable input
Out[36]=
Make a list of 20 random choices:
In[37]:=
Click for copyable input
Out[37]=
Make 5 lists of 3 random choices:
In[38]:=
Click for copyable input
Out[38]=
RandomSample picks a random sample of elements from a list, never picking any particular element more than once.
Pick 20 elements from the range 1 to 100, never picking any number more than once:
In[39]:=
Click for copyable input
Out[39]=
If you dont say how many elements to pick you get a random ordering of the whole list:
In[40]:=
Click for copyable input
Out[40]=
30.1Use Thread to make a list of rules with each letter of the alphabet going to its position in the alphabet. »
Expected output:
Out[]=
30.2Make a 4×6 grid of the first 24 letters of the alphabet. »
Expected output:
Out[]=
30.3Make a grid of the digits in 2^1000, with 50 digits per row, and put frames around everything. »
Expected output:
Out[]=
30.4Make a grid of the first 400 characters in the Wikipedia article for computers, with 20 characters per row, and frames around everything. »
Sample expected output:
Out[]=
Expected output:
Out[]=
Expected output:
Out[]=
Expected output:
Out[]=
30.8Find the lengths of the longest sequences of identical digits in 2^n for n up to 100. »
Expected output:
Out[]=
30.9Take the names of integers up to 100 and gather them into sublists according to their first letters. »
Expected output:
Out[]=
30.10Sort the first 50 words in WordList[ ] by their last letters. »
Sample expected output:
Out[]=
30.11Make a list of the first 20 squares, sorted by their first digits. »
Expected output:
Out[]=
30.12Sort integers up to 20 by the length of their names in English. »
Expected output:
Out[]=
30.13Get a random sample of 20 words from WordList[ ], and gather them into sublists by length. »
Sample expected output:
Out[]=
30.14Find letters that appear in Ukrainian but not Russian. »
Expected output:
Out[]=
30.15Use Intersection to find numbers that appear both among the first 100 squares and cubes. »
Expected output:
Out[]=
Expected output:
Out[]=
30.17Make a grid in which all possible permutations of the numbers 1 through 4 appear as successive columns. »
Expected output:
Out[]=
30.18Make a list of all the different strings that can be obtained by permuting the characters in hello»
Expected output:
Out[]=
30.19Make an array plot of the sequence of possible 5-tuples of 0 and 1. »
Expected output:
Out[]=
30.20Generate a list of 10 random sequences of 5 letters. »
Sample expected output:
Out[]=
30.21Find a simpler form for Flatten[Table[{i, j, k}, {i, 2}, {j, 2}, {k, 2}], 2]. »
Expected output:
Out[]=
+30.1Make an array plot from the numbers of the letters in the first 1000 characters in the Wikipedia article on computers, with 30 letters per row. »
Sample expected output:
Out[]=
+30.2Gather integers up to 30 into lists based on their values modulo 3. »
Expected output:
Out[]=
+30.3Gather the first 50 powers of 2 according to their last digits. »
Expected output:
Out[]=
+30.4Make a line plot of the result of sorting the numbers from 10 to +10 by their absolute values. »
Expected output:
Out[]=
+30.5Make a line plot of the list of the first 200 squares, sorted by their first digits. »
Expected output:
Out[]=
+30.6Make a line plot of integers up to 200 sorted by the lengths of their names in English. »
Expected output:
Out[]=
+30.7Get a random sample of 25 words from WordList[]»
Sample expected output:
Out[]=
+30.8Riffle periods into the string UNCLE to make U.N.C.L.E.»
Expected output:
Out[]=
+30.9Find letters that appear in Swedish or Polish but not English. »
Expected output:
Out[]=
+30.10Find countries that are in the World Health Organization but not the United Nations. »
Expected output:
Out[]=
+30.11Make a list of all pair-wise blends of red, green and blue. »
Expected output:
Out[]=
+30.12Make a list of array plots, each 50 rows high and with image size 50, of successive possible 8-tuples of 0 and 1. »
Expected output:
Out[]=
+30.13Generate 1000 random sequences of 4 letters, and make a list of ones that appear in WordList[ ].  »
Sample expected output:
Out[]=
What does Partition do if the blocks dont fit perfectly?
  • Transpose can be thought of as transposing rows and columns in a matrix.
  • ArrayFlatten flattens an array of arrays into a single array, or alternatively, a matrix of matrices into a single matrix.
  • DeleteDuplicates[list] does the same as Union[list], except it doesnt reorder elements.
 
Download Notebook Version
es