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

# insert -- copy a list, inserting an element

- Usage:
    `insert(i, x, L)`
- Inputs:
  - i, an integer,
  - x, a thing,
  - L, a basic list,
- Outputs:
  - L2, a basic list, a copy of L in which x has been inserted into position i

## Description

    i1 : L = 0..10

    o1 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    o1 : Sequence

    i2 : insert(4, "hi", L)

    o2 = (0, 1, 2, 3, hi, 4, 5, 6, 7, 8, 9, 10)

    o2 : Sequence

    i3 : insert(0, "hi", L)

    o3 = (hi, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    o3 : Sequence

    i4 : insert(11, "hi", L)

    o4 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, hi)

    o4 : Sequence

    i5 : insert(-1, "hi", L)

    o5 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, hi)

    o5 : Sequence

    i6 : apply({-1,-3,-5}, i -> L = insert(i, "hi", L)); L

    o7 = (0, 1, 2, 3, 4, 5, 6, 7, 8, hi, 9, hi, 10, hi)

    o7 : Sequence

## See also

- delete -- delete some elements of a list
- mingle -- mingle elements of several lists
- switch -- copy a list, switching two elements
- lists and sequences -- a detailed overview of lists and sequences in Macaulay2

## Ways to use insert:

- `insert(ZZ,Thing,BasicList)`

## For the programmer

The object insert is a method function.

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