Reference · Lists

last

signature
last(list | null) -> any

What it does

The last item of a list, or null when the list is empty. null counts as an empty list. xs[-1] is the same thing for a list that is known to have an item.

Examples

print(last([1, 2, 3]))print(last(["ann", "bob"]))print(last(split("a/b/c.txt", "/")))
output
3bobc.txt

Edge cases

print(last([]), last(null), last(null) ?? "empty")print(last([1, null]), len([1, null]))print(last([7]) == first([7]))
output
null null emptynull 2true

Errors

An argument that is not a list or null is a runtime error, not an error value. A string is not a list: use slice with a negative start for its last characters.

this one fails
print(last("abc"))
output
line 1: `last` expects a list as argument 1, got str

Chaining

In a pipeline last takes the list and needs no arguments: the largest after a sort, the newest of a list in order of time.

print([5, 3, 9] | sort | last)print(["pear", "fig", "apple"] | sort_by(len) | last)print("a.tar.gz" | split(".") | last)
output
9applegz

See also

first, max, drop, slice, reverse

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On last(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo