Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » lists and sequences » append
next | previous | forward | backward | up | index | toc

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

- Usage:
    `append(L, x)`
- Inputs:
  - L, a basic list,
  - x, a thing,
- Outputs:
  - a basic list, 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.

## See also

- insert -- copy a list, inserting an element
- join -- join lists, sequences, and iterable objects
- prepend -- add an element to the beginning of a list
- lists and sequences -- a detailed overview of lists and sequences in Macaulay2

## Ways to use append:

- `append(BasicList,Thing)`

## For the programmer

The object append is a compiled function.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/functions/append-doc.m2:54:0.