Last

Last[expr]

gives the last element in expr.

Last[expr,def]

gives the last element if there are any elements, or def otherwise.

Details

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

Examples

open allclose all

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 x if there are zero elements:

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

Generalizations & Extensions  (1)

Properties & Relations  (1)

Possible Issues  (2)

See Also

Part  First  Take  Most

Tutorials

Introduced in 1988
(1.0)
| Updated in 2015
(10.3)