List Manipulation

To access an element of a list we can use the Part command or the [[ ]] operator.
Part[ list, k] gives the kth element of the list.

In[12]:=

  list= { 1,2,4,8,16,32};
  Part[ list,4]

Out[12]=

  8

In[13]:=

  list[[5]]

Out[13]=

  16

To obtain more than one element, use Take.
Take[ list, k] makes a new list out of the first k elements of the argument. If k
is negative then the last k elements are taken. To obtain the sublist of elements
from position i through position j, use the command
Take[ list, {i,j}]
Similarly, Drop [list,k] makes a new
list by removing the first k elements ( or last |k| elements if k is negative).

In[14]:=

  list= { 1,3,5,7,9,10}
  Take[ list,2]

Out[14]=

  {1, 3, 5, 7, 9, 10}

Out[15]=

  {1, 3}

In[16]:=

  Take[ list, { 2, 3}]

Out[16]=

  {3, 5}

We illustrate this with an example program which takes a list and returns a new list
consistingof pairs of elements from the first list. If the input is { 1,3,5,7,9,11},
then the output is { { 1,3}, { 5,7}, { 9,11} }.

In[17]:=

  makepairs[l_]:=Module[ {newlist={},i=1},
                        While[i< Length[l],
                          AppendTo[newlist,
                                    Take[l, {i,i+1}]];
                          i=i+2;];
                         Return[newlist]]
                                
                          
                        

In[18]:=

  makepairs[ { 1,3,5,7,9,11}]

Out[18]=

  {{1, 3}, {5, 7}, {9, 11}}

In[19]:=

  makepairs[ { 1,3,5,7,9,11,13}]

Out[19]=

  {{1, 3}, {5, 7}, {9, 11}}

Exercise: Notice that if the list has an odd number of elements, then the last element
is dropped. Modify the program so that the last element is included at the end of the
resulting list, possibly by adding a zero to the list if the length is odd.

The same result can also be obtained by the Mathematica function Partition.
Partition[ list,n] breaks the list into sublists of n elements.

In[20]:=

  Partition[ { 1,3,5,7,9,11,13}, 2]

Out[20]=

  {{1, 3}, {5, 7}, {9, 11}}

Exercise: Modify the function above to write a function that Partitions a list into sublists
of n elements. If the number of elements is not a multiple of n, then add enough zeros
to make it a multiple of n.

If a list has sublists, then the elements can be accessed using the Part or [[ ]] commands.
The ith sublist can be obtained by [[i]], and then the jth element of this can be accessed.
A shortcut is to use [[ i,j]], which returns the jth element of the ith sublist.

In[21]:=

  list= { { 1,3}, { 5,7}, { 9, 11}};
  list[[ 2,2]]

Out[21]=

  7

A list consisting of sublists of equal length is a matrix, and matrix operations can be
applied to this. Transpose interchanges the rows and columns of a matrix. Recall that
FactorInteger returns the prime factorization of an integer, consisting of a list of lists,
where each sublist is of the form { p, e} where p^e is the term corresponding to
p in the prime factorization of n. Using Transpose, we can write a function to extract
the primefactors of an integer.

In[22]:=

  primefactors[n_]:= First[Transpose[ FactorInteger[n]]];

In[23]:=

  primefactors[ 879312]

Out[23]=

  {2, 3, 7, 2617}

Exercise: Write a function to extract all the exponents in the prime factorization. A number
n is squarefree if all the exponents are equal to 1. Write a function to check if all the exponents
are 1, and hence to detect if a number is squarefree.

Additional list manipulation functions are Union, Join, Intersection, and Complement. We
discussed Union and Join in the previous section. Complement has the following
format.
Complement[ universal, list1,list2, ..] removes all occurrences of elements
of list1, list2, and so on from universal.

Suppose we wish to find the number of integers less than 300 that are multiples of 3
but are not multiples of 7. We can make a table of the multiples of 3 and 7, then
use Complement and count the number. (There are better ways than this, but it does
illustrate the use of these functions).

In[24]:=

  a= Table[ 3i, { i, 1, Floor[ 300/3]}];
  b=Table[ 7 i, { i, 1,Floor[ 300/7]}];
  result=Length[ Complement[ a,b]]

Out[24]=

  86

Exercise: Write a function to reverse a list.

Exercise: Write a function to rotate a list by a specified number of elements either
clockwise or counterclockwise. For example, the result of rotating
{ 1,2,3,4,5,6,7} by 2 elements should be { 3,4,5,6,7,1,2}.

Up to Lists