Trim a vector in place

I am trying to delete the last n elements from a vector v. Is there a better way?

for _ in 1:n
    pop!(v)
end

pop! would not accept a range argument.

I try to avoid assuming anything about the indicies after:

resize!(v, length(v) - n)

More generally, to delete any sequence of indices, you could use deleteat!.

3 Likes