Lists

Lists are the most important objects in Mathematica. They appear everywhere, visibly or hidden. Once you learn to use them properly, it will make working with the program that much simpler.

Lists

Creating One-Dimensional Lists

Notations for Lists

Lists are objects whose elements are placed into curly braces.

{1, 4, 9}

List[1, 4, 9]

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

List[List[1, 2, 3], List[4, 5, 6], List[7, 8, 9]]

List[1, 4, 9] == {1, 4, 9}

{1, 4, 9}

{1, 4, 9} // FullForm

FullForm[{1, 4, 9}]

Side notes on FullForm:

(Sqrt[a]) // FullForm

(a^((1/2)))

Hold[∫x (Sqrt[ x]) \[DifferentialD]x] // FullForm

Lists collect objects together

{2, 3, 4}

\(x\^{2, 3, 4} - 1\)

\(x\^% - 1\)

D[{-1 + (x^2), -1 + (x^3), -1 + (x^4)}, x]

D[%, x]

% /. x -> 3

Lists help to store and manipulate tables

The Mathematica function Table is useful for creating lists. It evaluates an expression for different values of an iterator.

Table[(i^2), {i, 5, 25}]

The list in the second argument of Table contains the name of the iterator and further information. The following forms are possible (i is the iterator):
• {n} creates n identical entries (with no iterator present)
• {i,n} varies i over 1, 2, …, n
• {i,a,n} varies i over a, a+1, …, n
• {i,a,n,s} varies i over a, a+s, a+2s, …, \(TraditionalForm\`\(n\^*\)\), where \(TraditionalForm\`\(n\^*\)\)⩽n

Table[a, {10}]

Table[(i^2), {i, 0, 10, 1}]

Table[(i^2), {i, 0, 1, 0.3}]

Table[f!, {f, 1, 5}]

f[x_] := Sin[x]

?f

Table[f[x], {x, 1, 4}]

% // N

Table[Sin[(x/5)], {x, 0, 4}]

N[%]

Table[Sqrt[x], {x, 0, 1, 0.25}]

\(exprList = %\^2 + 3\)

Side note: Formatting of tables

% // TableForm

?*Form

exprList // ColumnForm

exprList // MatrixForm

Table-related functions: Product, Sum, Range

Table[(x^i) + 2  i, {i, 5}]

% // TableForm

Table[(x^i) + 2  i, {i, 5}]

Product[(x^i) + 2  i, {i, 5}]

(∏_(i = 1)^5)((x^i) + 2  i)

Sum[(x^i) + 2  i, {i, 5}]

(∑_(i = 1)^5)((x^i) + 2  i)

Arithmetic sequences can be created more easily using Range. No name is needed for the iterator in this case.

Table[i, {i, 5}]

Range[10]

Range[0, 10]

Range[0, 1,  .3]

Range[x, x + 5]

% /. {x -> 3}

Extracting and Accessing List Elements

Introductory Exercises

Our exercise object is:

\(\(motto = {"Computer", "science", "is", "a", "lot", "of", "fun"};\)\)

The function Length determines its length (what a surprise!).

Length[motto]

motto[[5]]

motto[[2]]

Or in StandardForm:

motto[[5]]

motto[[2]]

Table[Random[Integer, {1, 5}], {100}]

randomElement[pickList_List] := 
      pickList[[Random[
          Integer, {1, Length[pickList]}]]]

Table[randomElement[motto], {100}]

Table[randomElement[{1, 2, 3}], {100}]

randomElement[pickList_List] := 
      pickList[[Random[
          Integer, {1, Length[pickList]}]]]

Table[
      motto[[n]], {n, 1, 
        Length[motto]}]

Table[randomElement[motto], {50}]

First

First[motto]

This is, of course, equivalent to

motto[[1]]

Last

Last[motto]

This is equivalent to

motto[[Length[motto]]]

and equivalent to

motto[[-1]]

Table[
      motto[[
        n]], {n, -1, -7, -1}]

Part

Announcement: No Class on Thursday!

f[g[x], y, 
        h[k]][[2]]

Table[Part[f[g[x], y, h[k]], m], {m, 1, 3}]

motto = {a, b, c, d, e};

lst = {"abc", f[g], 3, Pi}

Part[motto, 2]

motto[[2]]

motto[[2]]

Table[
      Part[motto, Random[Integer, {1, Length[motto]}]], {100}]

Negative values count from the end of the list.

Part[motto, -1]

Part[motto, -2]

This picks the list elements from right to left, starting at the end of the list:

Table[Part[motto, n], {n, -1, -7, -1}]

Note: There is the built-in Reverse command that performs this operation:

Reverse[motto]

motto = {"Computer", "science", "is", "a", "lot", "\
of", "fun"}

Part[motto, 0]

Head[motto]

Part[f[x, y], 0]

motto[[0]]

Part[a + b, 0]

FullForm[a + b]

Plus[a, b, c, d, e, f]

Part[Pi*Pi, 0]

\(x\^{1, 2, 3}\)

Part[motto, {1, 2, 3}]

Part[motto, Table[i, {i, 1, 7, 2}]]

Part[motto, Range[3]]

motto[[{1, 3, 5, 7}]]

motto[[Range[3]]]

motto[[Range[4, 7]]]

Part[motto, Range[3]]

Part[motto, Table[n, {n, -1, -3, -1}]]

Part[motto, -Range[3]]

Table[Random[Integer, {-10, 10}], {100}]

motto = {"Computer", "science", "is", "a", "lot", "\
of", "fun"}

motto[[2]]

motto[[2]] = "programming"

motto

ReplacePart[motto, "work", 7]

ReplacePart[motto, "fun", 7]

Take

Take can extract entire sublists. A positive number as second argument results in the same number of elements from the left. Negative numbers count the elements from the right. If we pass a list with a start and end number in the second argument, Take returns the corresponding sublist.

motto = {"Computer", "science", "is", "a", "lot", "\
of", "fun"}

Take[motto, 3]

Take[motto, -4]

Take[motto, {2, 5}]

Take[motto, {1, 7, 2}]

Drop

Drop functions analogously and throws the corresponding elements away.

motto = {"Computer", "science", "is", "a", "lot", "\
of", "fun"}

Drop[motto, 3]

Drop[motto, -3]

Drop[motto, {4, 6}]

Drop[motto, 2]

Drop[motto, {2}]

Drop[motto, {2, 7, 2}]

Drop[Range[7], {2, 5, 2}]

Drop[motto, {2, 5, 2}]

Rest

Rest[motto]

Drop[motto, 1]

Extract

Take[motto, {2}]

Extract[motto, 2]

Extract[motto, {{1}, {2}, {7}}]

... they work on ALL expressions

First[{a, b, c, d}]

FullForm[{a, b, c, d}]

poly = (x^2) +  3 x  -  5

First[(x^2) +  3 x  -  5]

FullForm[(x^2) +  3 x  -  5]

poly2 = Hold[(x^2) +  3 x  -  5]

poly2

Head[poly2]

?Attributes

Attributes[Plot]

Attributes[poly2] = {HoldAll}

poly2

First[poly2]

poly[[3]] // FullForm

Drop[poly, -1]

poly[[2]] = 5 Pi

poly

ReplacePart[poly, (x^3), 3]