Legacy Documentation

Mathematica® Teacher's Edition (2002)

This is documentation for an obsolete product.
Current products and services
 Documentation /  Mathematica Teacher's Edition /  The Teacher's Book /  Basic Calculations /  One Thing after Another /

3.5 Manipulating Elements of Lists

Many of the most powerful list manipulation operations in Mathematica TE treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list.
You can refer to an element of a Mathematica TE list by giving its "index". The elements are numbered in order, starting at 1.

Operations on list elements.

This extracts the second element of the list.

In[1]:= {5, 8, 6, 9}[[2]]

Out[1]=

This extracts a list of elements.

In[2]:= {5, 8, 6, 9}[[ {3, 1, 3, 2, 4} ]]

Out[2]=

This assigns the value of v to be a list.

In[3]:= v = {2, 4, 7}

Out[3]=

You can extract elements of v.

In[4]:= v[[ 2 ]]

Out[4]=

By assigning a variable to be a list, you can use Mathematica TE lists much like "arrays" in other computer languages. Thus, for example, you can reset an element of a list by assigning a value to v[[i]].

Arraylike operations on lists.

Here is a list.

In[5]:= v = {4, -1, 8, 7}

Out[5]=

This resets the third element of the list.

In[6]:= v[[3]] = 0

Out[6]=

Now the list assigned to v has been modified.

In[7]:= v

Out[7]=

The Table function generates lists from formulas.

This gives a list of the values of , with running from 1 to 6.

In[8]:= Table[i^2, {i, 6}]

Out[8]=

You can also create lists of formulas.

In[9]:= Table[x^i, {i, 5}]

Out[9]=