length()
Return the length of a list expression.
Synopsis
length(list-expression)
Description
The length()
function is used to find the number of elements in a list.
Since lists are 0-indexed, the index of the last item in a list of length
\(n\) is \(n-1\).
Parameters
list-expression
an expression that evaluates to a list
Returns
The length of the list expression.
Examples
Find the length of a list. This returns 3
.
length(list(1,1,1))
Find the value of the last element in a list X
. This sets last_item
to 5
.
X = list(1,2,3,4,5)
last_item = X[length(X)-1]
Errors
It is an error if the argument to length()
is not a list-expression.