# append -- add an element to the end of a list

## Synopsis

• Usage:
append(L, x)
• Inputs:
• L, ,
• x, ,
• Outputs:
• , the list obtained by adding the element x to the end of list L

## Description

 i1 : append({1, 7, 8, 3}, 3) o1 = {1, 7, 8, 3, 3} o1 : List i2 : L = {"old", "old", "old"}; i3 : append(L, "new") o3 = {old, old, old, new} o3 : List

The new list will be of the same class as L.

 i4 : K = (a, b, c); i5 : append(K, z) o5 = (a, b, c, z) o5 : Sequence

Only a single element can be appended with this function. To append the elements of a list, use join. To add the new element to the beginning of the list, or at a particular index, use prepend or insert, respectively.

 i6 : join(K, (x, y, z)) o6 = (a, b, c, x, y, z) o6 : Sequence i7 : prepend(z, K) o7 = (z, a, b, c) o7 : Sequence i8 : insert(1, z, K) o8 = (a, z, b, c) o8 : Sequence

Append always returns a new list, rather than modifying the input list, even if L is a MutableList.

 i9 : L = new MutableList from {2,3,5}; i10 : peek append(L, 7) o10 = MutableList{2, 3, 5, 7} i11 : peek L o11 = MutableList{2, 3, 5}

Notice that the order of the arguments is switched in prepend versus append: we write prepend(x, L) and append(L, x). A good way to remember this is that the new element is visually placed before or after the list, depending on where we want it to appear in the output.