This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / A Practical Introduction to Mathematica / Lists /

1.8.4 Getting Pieces of Lists

Picking out elements of lists.

We will use this list for the examples.

In[1]:= t = {a,b,c,d,e,f,g}

Out[1]=

Here is the last element of t.

In[2]:= Last[t]

Out[2]=

This gives the third element.

In[3]:= t[[3]]

Out[3]=

This gives a list of the first and fourth elements.

In[4]:= t[[ {1, 4} ]]

Out[4]=

Picking out sequences in lists.

This gives the first three elements of the list t defined above.

In[5]:= Take[t, 3]

Out[5]=

This gives the last three elements.

In[6]:= Take[t, -3]

Out[6]=

This gives elements 2 through 5 inclusive.

In[7]:= Take[t, {2, 5}]

Out[7]=

This gives elements 3 through 7 in steps of 2.

In[8]:= Take[t, {3, 7, 2}]

Out[8]=

This gives t with the first element dropped.

In[9]:= Rest[t]

Out[9]=

This gives t with its first three elements dropped.

In[10]:= Drop[t, 3]

Out[10]=

This gives t with only its third element dropped.

In[11]:= Drop[t, {3, 3}]

Out[11]=

Section 2.1.5 shows how all the functions in this section can be generalized to work not only on lists, but on any Mathematica expressions.

The functions in this section allow you to pick out pieces that occur at particular positions in lists. Section 2.3.2 shows how you can use functions like Select and Cases to pick out elements of lists based not on their positions, but instead on their properties.