Sidechair

array.remove()

Browse the reference

Deletes the item at a position and returns it; later items move down to fill the gap.

Try it in the editor

array.remove(list, 2) takes out the third item. A negative index counts back from the end in v6, so array.remove(list, -1) removes the last item, the same as array.pop().

An index outside the array stops the script with an error naming the index and the array size.

Syntax

Pine Script
array.remove(id: any array type, index: series int) -> series <type>
array.remove(index: series int) -> series <type>

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to take an item out of. Later items move down to close the gap.
indexseries intrequiredPosition of the item. Counts from 0; a negative index counts back from the end, so -1 is the last item.

Returns

series <type>: The item that was removed.

Remarks

  • Removing inside a loop over the same array shifts the positions of the items still to come. Loop from the end toward 0 when you remove as you go.

Example

Pine ScriptDrop levels once price crosses themOpen in editor
//@version=6
indicator("array.remove: crossed levels", overlay = true)
var levels = array.from(close * 0.99, close * 1.01, close * 1.02)
i = array.size(levels) - 1
while i >= 0
    if close > array.get(levels, i)
        array.remove(levels, i)
    i -= 1
plot(array.size(levels), "Levels left")

See also

Runs on Sidechair