Creating, Accessing, and Modifying Lists

Math 250 - Mathematical Computing
Christopher Hanusa

3.1 Aim

We develop tools for creating, accessing, and modifying lists.

Tip: You can evaluate ALL cells in this file in order using the Evaluation > Evaluate Notebook command.

3.2 The Table command, Part Deux

More Complicated Functions

The Table command is much more powerful than simply creating lists of numbers.  For instance, you can create lists of anything in the language of Mathematica:

Graphics objects:

Table[Plot[x^n,{x,-2,2}],{n,0,10}]

Colors:

colors=Table[ColorData["Rainbow"][x],{x,0,1,.05}]

Length[colors]

The Periodic Table of the Elements:

Table[ElementData[i,"Abbreviation"],{i,1,100}]

Coordinates of the vertices of a polygon:

vertices=Table[{Cos[2π k/6],Sin[2π k/6]},{k,0,5}]
Graphics[Polygon[vertices]]

vertices=Table[{Cos[2π k/5],Sin[2π k/5]},{k,0,4}]
Graphics[Polygon[vertices]]

A list of such a polygons:

Table[Graphics[Polygon[Table[{Cos[2π k/i],Sin[2π k/i]},{k,0,i-1}]]],{i,3,10}]

More Variables

The Table command can also create a list as a function of two or more variables changing at a time.

array=Table[x*y,{x,3},{y,5}]

Read this Table command as:

"Create a table of values of the form x times y, where x ranges from 1 to 3 and y ranges from 1 to 5.”

Example 3.2.1. What do the following commands do?

Table[{x,y},{x,2,4},{y,5,7}]

Table[IntegerString[x,"Roman"],{x,100}]

Example 3.2.2. How do you generate the following output?

{{{0,0},{0,1},{0,2}},
{{1,0},{1,1},{1,2}},
{{2,0},{2,1},{2,2}}}

Table[{x,y,z},{x,0,2},{y,0,2},{z,0,2}]

IntegerName takes a number and gives out its English name.

Table[IntegerName[x],{x,100}]

{10,100,1000,10000}

{10^1,10^2,10^3,10^4}

Table[10^i,{i,4}]

Table[IntegerName[10^i,"Words"],{i,4}]

{"ten","one hundred","one thousand","ten thousand"}

3.3 Flatten

The number of braces that can accumulate in Mathematica can be overwhelming.
When you want to remove extra braces from nested lists, use the Flatten command.

list=Table[x*y,{x,3},{y,3}]

Flatten[list]

You can also specify how many layers of braces you want to remove.  For example, if your goal is to create a set of coordinate pairs (for points on a picture, for instance), then you wouldn’t want to remove all of the braces.

list2=Table[{x,y},{x,5},{y,3}]

Flatten[list2]

Instead, use the optional second argument to only remove one set of braces.

coords=Flatten[list2,1]

Graphics[Point[coords]]

Example 3.3.1. What will this command give?

Flatten[{{{{1},2},3},4}]

Example 3.3.2. What command will give {{{1},2},3,4}?

Flatten[{{{{1},2},3},4},3]

3.4 Join

How do you combine two lists?  Use Join.

list1={a,b,c}
list2={1,2,3}

Join[list1,list2]

Example 3.4.1.  Create a list with the first 10 positive even numbers followed by the first 10 positive odd numbers.

evens=Table[2*i,{i,1,10}]

odds=Table[2*i-1,{i,1,10}]

Join[evens,odds]

3.5 Append and Prepend

To add an element to the end of a list, use Append.  To add an element to the beginning, use Prepend.

powersOfTen=Table[10^i,{i,0,5}]

Append[powersOfTen,10^6]

powersOfTen

Prepend[powersOfTen,10^-1]

powersOfTen

You can see that powersOfTen does not change when using Append or Prepend. 
Either redefine powersOfTen or use AppendTo or PrependTo.

powersOfTen=Append[powersOfTen,10^6]

PrependTo[powersOfTen,10^-1]

powersOfTen

Example 3.5.1. What will the following command do?

Append[{1,2,3,4},{5,6}]

Join[{1,2,3,4},{5,6}]

Flatten[Append[{1,2,3,4},{5,6}]]

3.6 Accessing parts of lists

One entry of a list

It is often useful to access one or more of the entries of a list.  For this we use double square brackets [[n]], which is the shorthand notation for the command Part.

This is the list of the first 100 prime numbers.

primes=Table[Prime[n],{n,100}]

What is the 40th prime number?

primes[[40]]

Prime[40]

Part[primes,40]

One entry of an array

When the list is a nested list, you can reference an entry of the list to choose a sublist, or any entry of any sublist.

nestedList={{1,2},{3,4},{5,6}};

nestedList[[2]]

nestedList[[2]][[1]]

nestedList[[2,1]]

A deeper nested list

veryverydeeplist = {{{{{a,b},{c,d}},{{e,f},{g,h}}},{{{i,j},{k,l}},{{m,n},{o,p}}}},{{{{q,r},{s,t}},{{u,v},{w,x}}},{{{y,z},{α,β}},{{γ,δ},{ϵ,ζ}}}}}

veryverydeeplist[[2]]

veryverydeeplist[[2,2]]

veryverydeeplist[[2,2,1]]

veryverydeeplist[[2,2,1,2]]

veryverydeeplist[[2,2,1,2,1]]

Multiple entries of a list

To extract multiple entries from a list, you can put a list of indices in the double square brackets.

primes[[{3,5,7}]]

primes[[Range[3,7]]]

Multiple entries of an array

To choose certain rows and columns of an array you can also use sets.  If you want all of the rows and only some of the columns (or vice versa), use the variable “All”.

products=Table[x*y^2,{x,1,10},{y,1,10}];
TableForm[products]

Here we choose rows three, four and five, and columns 2, 4, and 8:

products[[Range[3,5],{2,4,8}]]

Here we choose the second entries of every row:

products[[All,2]]

Or the third entry of every column:

products[[3,All]]