Introduction to Lists

Math 245 - Mathematical Models
Christopher Hanusa

Aim

In Mathematica, the key data structure is the list.  Whenever multiple numbers are to be grouped together into one object, this object will be a list.  For example, if you wish to reference the point (2,1) in the (x,y) plane, you would use write it as the list {2,1}.  Every list is surrounded by a left brace { and a right brace }.  Entries of the list are separated by commas.

The aim of this tutorial is to introduce the user to lists, highlight important commands which generate lists, and show the user a few of the commands which manipulate lists.

These tutorials are meant to be interactive.  You should be playing around with the inputs to try to see what changes in the output when you change the inputs.

The Table command

The most important command to master first is the Table command.  This command will generate a list based on a function you specify.  
First, let's analyze the structure of a Table command:

Table[x^2,{x,0,5}]

There are always two inputs, the function (x^2) and the list that specifies the values of x that will be inserted into the function. 
The first element in the list is always the variable is modified by the function.  
	[Notice that this is automatically colored green in this list and in the function for your convenience.]
Depending on what this list contains, the values of this variable are specified in different ways.
If the list is of length two, such as {x,5}, then the values of x that will be inserted are between 1 and that value (5).

Table[x^2,{x,5}]

If the list is of length three, such as {x,-1,5}, then the values of x that will be inserted are from the first number to the second by increments of 1.

Table[x^2,{x,-1,5}]

If the list is of length four, such as {x,0,10,2}, then the values of x that will be inserted are from the first number to the second number by increments of the last number.

Table[x,{x,0,10,2}]

If the list is contains a variable and another nested list, such as {x,{0,10,2}}, then the values of x that will be inserted are exactly the values in this nested list.

Table[x^2,{x,{0,10,2}}]

Slightly more advanced: The Table command can also create a list with two or more variables changing at a time.  Simply put two variable ranges as inputs.  In the following example, we consider all values of x from 1 to 3 and all values of y from 1 to 3 and multiply them together (because that's what the function x*y says to do).

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

The TableForm command

The TableForm command will take a list (or list of lists) and display them in a tabular environment, which is sometimes convenient when you want to look at your data.  In the following example, we take the nested list from the previous example, define it as the variable list, and show it in a tabular form.

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

If you would like more examples of how to use Table, search the Documentation Center for Table or "tutorial/MakingTablesOfValues"

Comprehension Questions:

1. For each of the following Table commands, complete the following sub-questions.
(a) BEFORE EVALUATING THE COMMAND, what list do you expect the command to give?  Or do you expect an error?
(b) Now, evaluate the command.  Did it do what you expect it to do?
(c) If not, figure out what went wrong with your reasoning.

Table[i,{i,10}]

Table[y^2,{x,5}]

Table[i,{2,6}]

Table[x^2,{x,Pi,10}]

Table[-z,{z,1.5,3.5,0.5}]

Table[a,{a,1,10,Pi}]

Table[Sqrt[b],{b,{1,10,Pi}}]

Table[{x,x^2},{x,3,6}]

Table[{s,7,9},{t,3,5}]

Table[{x,y},{x,2,5},{y,0,1}]

Discuss these answers with your neighbors.

2. Determine which Table commands give the following lists.  There are multiple solutions for each one.

{1,2,3,4,5,6}

{1, 4, 7, 10, 13, 16, 19}

{2 π,2+2 π,4+2 π,6+2 π,8+2 π}

{(1/4),(1/5),(1/6),(1/7)}

{(1/10),(1/2),-(1/4)}

{{-3,9},{-2,4},{-1,1},{0,0}}

{{1,2,3,4},{2,3,4,5}}

Solutions

Table[i,{i,6}]

Table[i,{i,1,19,3}]

Table[2(Pi+i),{i,0,4}]

Table[1/i,{i,4,7}]

Table[1/i,{i,{10,2,-4}}]

Table[{i,i^2},{i,-3,0,1}]

Table[i+j,{i,2},{j,4}]

3. Investigate the possibilities of what happens when you roll two dice.  What is their sum?  What is their product?  Evaluate and use these provided Table commands:

AddDice=Table[i+j,{i,1,6},{j,1,6}]
MultDice = Table[i*j,{i,1,6},{j,1,6}]

Does this do what you expect?  Now use TableForm to output AddDice and MultDice in a more readable format.

Other useful list commands

Length and Total

Two self-explanatory functions are Length and Total.  Length will tell you how many elements there are in the list.  Total will sum the values in the list.

Length[{1,2,3}]

Length[{1,{2,3}}]

Total[{1,2,3}]

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

Total[powersOfTen]

Parts of a list

It is often useful to single out one or more of the entries of a list.  For this we use double square brackets [[n]], (same as Part).

powersOfTen[[5]]

Part[powersOfTen,5]

When the list is a nested list, you can reference an entry (a sublist) or even an entry of the sublist.

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

nestedList[[2]]

nestedList[[2,1]]

Flatten

When there are nested lists and you would like to remove some of the extra braces, you should use Flatten.

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

Flatten[list]

You can also specify how many layers of braces you want to remove.

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

Flatten[list2]

Flatten[list2,1]

Append

When you want to add elements to a preexisting list, use Append.  This is often used in conjunction with redefining the preexisting list.

powersOfTen

Append[powersOfTen,10^6]

powersOfTen

You can see that powersOfTen did not change when using Append. 
You need to redefine powersOfTen if you want to actually add an entry to the list for future use.

powersOfTen=Append[powersOfTen,10^6]

powersOfTen

Comprehension Questions:

4. For each of the following commands, complete the following sub-questions.
(a) What list do you expect the command to give?  Or do you expect an error?
(b) Now evaluate the command.  Did it do what you expect it to do?
(c) If not, figure out what went wrong with your reasoning.

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

Length[{{1,2,3}}]

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

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

Total[{{1,2,3}}]

Part[powersOfTen,6]

Part[powersOfTen,{1,3,5}]

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

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

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

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

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

Discuss these answers with your neighbors.

5. Here are two lists which represent the x- and y-coordinates of points, and an incorrect Table command that hopes to collect the points into a list of ordered x,y pairs.  Figure out what is wrong with the command and fix it.

x={1,2,3,4,5}
y={2,5,10,17,26}
points=Table[{x[i],y[i]},{i,1,Length[x]}]

Solution:

You need to use double brackets to reference parts of a list.

points=Table[{x[[i]],y[[i]]},{i,1,Length[x]}]