# Non-mutating deleteat/splice function

**URL:** https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139
**Category:** General Usage
**Created:** [May 25, 2018, 8:40am UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139 "2018-05-25T08:40:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [May 25, 2018, 8:40am UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139/1 "2018-05-25T08:40:53Z")

</div>

Is there a function to return a copy of an array with one or more elements removed? Or to splice in different elements into a copy of the array and return that?

I’ve been doing things like `deleteat!(copy(arr), idxs)`, but since this is such a basic feature I wondered if I was missing an existing function to do it directly.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 25, 2018, 10:54am UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139/2 "2018-05-25T10:54:49Z")

</div>

`getindex` (aka `[]`) and `vcat` should do what you want, but if you want to provide indexes in the format used by eg `deleteat!` you could use something like

```julia
julia> x = 1:5;

julia> x[setdiff(indices(x, 1), [2,3])]
3-element Array{Int64,1}:
 1
 4
 5

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [May 25, 2018, 12:59pm UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139/3 "2018-05-25T12:59:50Z")

</div>

The `deleteat!(copy(` solution actually seems faster than this method though.

```julia
julia> y = rand(1:100, 100);
julia> del_sd(a, idx) = a[setdiff(indices(a, 1), idx)];
julia> del_cp(a, idx) = deleteat!(copy(a), idx);
julia> @btime del_cp(y, (2, 3, 43))
  423.386 ns (1 allocation: 896 bytes)
97-element Array{Int64,1}:
 90
 88
 30
 41
 16
  ⋮
 22
 18
 78
 17

julia> @btime del_sd(y, (2, 3, 43))
  18.820 μs (25 allocations: 7.56 KiB)
97-element Array{Int64,1}:
 90
 88
 30
 41
 16
  ⋮
 22
 18
 78
 17

julia>

```

(AFAICT, a `getindex`+`vcat` type solution would get kinda unwieldly unless the indices to be removed are contiguous, and not as readable as the `deleteat!` version even for contiguous/single index cases.)

I’m ok with using the `deleteat!(copy(arr), idxs)` method, that just seemed a roundabout way to do this so I wondered if there was an easier more obvious way that I was missing.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 25, 2018, 1:37pm UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139/4 "2018-05-25T13:37:07Z")

</div>

The logit of the [`_deleteat!`](https://github.com/JuliaLang/julia/blob/master/base/array.jl#L1164-L1193) makes some assumptions which make it very efficient, so it may be worthwhile converting it to a non-modifying version.

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [May 25, 2018, 7:58pm UTC](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139/5 "2018-05-25T19:58:23Z")

</div>

What about

```julia
function deleteat(vec, idxs)
newlen = length(vec) - length(idxs)
vn = Vector{eltype(vec)}(newlen)
n=1
k=1
for idx in idxs
    while n < idx
        @inbounds vn[k]=vec[n]
        k+=1
        n+= 1
    end
    n+= 1
end
while k<=newlen
    @inbounds vn[k] = vec[n]
    k+=1
    n+=1
end
vn
end

```

Insert error checking (idxs must be sorted and unique) if you want. Roughly as fast as memcopy when deleting few elements, and significantly faster than taking a copy and then copying vast portions around again.

Luckily, in julia there is no reason to compose Base functions in weird ways when you can just write a straight loop 😉
