Getting Pieces of Lists

First[list]the first element in list
Last[list]the last element
Part[list,n] or list[[n]]the n^(th) element
Part[list,-n] or list[[-n]]the n^(th) element from the end
Part[list,m;;n]elements m through n
Part[list,{n1,n2,...}] or list[[{n1,n2,...}]]
the list of elements at positions , , ...

Picking out elements of lists.

We will use this list for the examples.
In[1]:=
Click for copyable input
Out[1]=
Here is the last element of .
In[2]:=
Click for copyable input
Out[2]=
This gives the third element.
In[3]:=
Click for copyable input
Out[3]=
This gives the list of elements through .
In[4]:=
Click for copyable input
Out[4]=
This gives a list of the first and fourth elements.
In[5]:=
Click for copyable input
Out[5]=
Take[list,n]the first n elements in list
Take[list,-n]the last n elements
Take[list,{m,n}]elements m through n (inclusive)
Rest[list]list with its first element dropped
Drop[list,n]list with its first n elements dropped
Most[list]list with its last element dropped
Drop[list,-n]list with its last n elements dropped
Drop[list,{m,n}]list with elements m through n dropped

Picking out sequences in lists.

This gives the first three elements of the list defined above.
In[6]:=
Click for copyable input
Out[6]=
This gives the last three elements.
In[7]:=
Click for copyable input
Out[7]=
This gives elements through inclusive.
In[8]:=
Click for copyable input
Out[8]=
This gives elements through in steps of .
In[9]:=
Click for copyable input
Out[9]=
This gives with the first element dropped.
In[10]:=
Click for copyable input
Out[10]=
This gives with its first three elements dropped.
In[11]:=
Click for copyable input
Out[11]=
This gives with only its third element dropped.
In[12]:=
Click for copyable input
Out[12]=

"Manipulating Expressions like Lists" shows how all the functions here can be generalized to work not only on lists, but on any Mathematica expressions.

The functions here allow you to pick out pieces that occur at particular positions in lists. "Finding Expressions That Match a Pattern" 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.

New to Mathematica? Find your learning path »
Have a question? Ask support »