({{, (RefLink[First, paclet:ref/First])[expr]
gives the first element in expr. }, {, (RefLink[First, paclet:ref/First])[expr,def]
gives the first element if it exists, or def otherwise.}})

OpenersNotesSection(Spacer1)Details

(RefLink[First, paclet:ref/First])[expr] is equivalent to expr[[1]].

(RefLink[First, paclet:ref/First]) has attribute (RefLink[HoldRest, paclet:ref/HoldRest]). In (RefLink[First, paclet:ref/First])[expr,def], def is evaluated only if it is to be returned.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (5)

Extract the first element of a list:

First[{a,b,c}]

Extract the first row of a matrix:

First[{{a,b},{c,d}}]

Extract the first element of an association:

First[<|1->a,2->b|>]

The head need not be (RefLink[List, paclet:ref/List]):

First[a^2+b^2]

Return x if there are zero elements:

First[{},x]

First[{a,b},x]

OpenersExampleSection1(Spacer1)Generalizations & Extensions  (2)

(RefLink[First, paclet:ref/First]) works on (RefLink[SparseArray, paclet:ref/SparseArray]) objects:

First[SparseArray[Range[100]]]

(RefLink[First, paclet:ref/First]) extracts the first value in an (RefLink[Association, paclet:ref/Association]):

First[<|a->x,b->y|>]

OpenersExampleSection2(Spacer1)Properties & Relations  (1)

(RefLink[First, paclet:ref/First])[expr] is equivalent to expr[[1]]:

First[{a,b,c}]

{a,b,c}[[1]]

OpenersExampleSection3(Spacer1)Possible Issues  (2)

The expression is evaluated before (RefLink[First, paclet:ref/First]) is applied:

First[b^2+a^2]

(RefLink[First, paclet:ref/First]) always operates on the (RefLink[FullForm, paclet:ref/FullForm]) of expressions:

First[1/a^2]

FullForm[1/a^2]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Part, paclet:ref/Part])  ▪  (RefLink[Last, paclet:ref/Last])  ▪  (RefLink[Rest, paclet:ref/Rest])  ▪  (RefLink[Take, paclet:ref/Take])  ▪  (RefLink[SelectFirst, paclet:ref/SelectFirst])  ▪  (RefLink[FirstPosition, paclet:ref/FirstPosition])  ▪  (RefLink[FirstCase, paclet:ref/FirstCase])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Getting Pieces of Lists, paclet:tutorial/GettingPiecesOfLists])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Elements of Lists, paclet:guide/ElementsOfLists])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Parts of Expressions, paclet:guide/PartsOfExpressions])}, {▪(RefLinkPlain[Binary Data, paclet:guide/BinaryData])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])(WebLink[: Operations on Lists, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])(WebLink[: Debugging Your Code, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])}})}})