First

First[expr]

gives the first element in expr.

First[expr,def]

gives the first element if it exists, or def otherwise.

Details

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

Examples

open allclose all

Basic Examples  (5)

Extract the first element of a list:

Extract the first row of a matrix:

Extract the first element of an association:

The head need not be List:

Return x if there are zero elements:

Generalizations & Extensions  (2)

First works on SparseArray objects:

First extracts the first value in an Association:

Properties & Relations  (1)

First[expr] is equivalent to expr[[1]]:

Possible Issues  (2)

The expression is evaluated before First is applied:

First always operates on the FullForm of expressions:

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