Last

Last[expr]
gives the last element in expr.

Last[expr,def]
gives the last element if there are any elements, or def otherwise.

DetailsDetails

  • Last[expr] is equivalent to .
  • Last has attribute HoldRest. In Last[expr,def], def is evaluated only if it is to be returned.

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

Extract the last element of a list:

In[1]:=
Click for copyable input
Out[1]=

Extract the last row of a matrix:

In[1]:=
Click for copyable input
Out[1]=

Extract the last element of an association:

In[1]:=
Click for copyable input
Out[1]=

The head need not be List:

In[1]:=
Click for copyable input
Out[1]=

Return if there are zero elements:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Introduced in 1988
(1.0)
| Updated in 2015
(10.3)
Translate this page: