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:

Wolfram Research (1988), First, Wolfram Language function, https://reference.wolfram.com/language/ref/First.html (updated 2015).

Text

Wolfram Research (1988), First, Wolfram Language function, https://reference.wolfram.com/language/ref/First.html (updated 2015).

CMS

Wolfram Language. 1988. "First." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/First.html.

APA

Wolfram Language. (1988). First. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/First.html

BibTeX

@misc{reference.wolfram_2024_first, author="Wolfram Research", title="{First}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/First.html}", note=[Accessed: 11-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_first, organization={Wolfram Research}, title={First}, year={2015}, url={https://reference.wolfram.com/language/ref/First.html}, note=[Accessed: 11-December-2024 ]}