# Equivalent of np.delete?

**URL:** <https://discourse.julialang.org/t/equivalent-of-np-delete/122642>\
**Category:** General Usage\
**Created:** [November 14, 2024, 4:44pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642 "2024-11-14T16:44:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [November 14, 2024, 4:44pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/1 "2024-11-14T16:44:32Z")

</div>

Hi,

I would like to do the same as:

```julia
import numpy as np

a = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], 
              [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]],
              [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]],
              [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]])

>>> a.shape
(4, 3, 2, 3)

new = np.delete(a, (0, 1), axis=3)

>>> new.shape
(4, 3, 2, 1)

```

Which gives:

```julia
array([[[[3],
         [6]],

        [[9],
         [12]],

        [[15],
         [18]]],

       [[[3],
         [6]],

        [[9],
         [12]],

        [[15],
         [18]]],

       [[[3],
         [6]],

        [[9],
         [12]],

        [[15],
         [18]]],

       [[[3],
         [6]],

        [[9],
         [12]],

        [[15],
         [18]]]])

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 14, 2024, 4:56pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/2 "2024-11-14T16:56:20Z")

</div>

> [@Ju\_ska](#):
>
> `new = np.delete(a, (0, 1), axis=3)`

I think the closest analog is something like:

```julia
new = a[:, :, filter(!in((1,2)), 1:end)]

```

I think there was some discussion of an “inverse” slice that lets you say which indices to exclude, and maybe a package for it, but I can’t find it at the moment.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 14, 2024, 5:11pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/3 "2024-11-14T17:11:30Z")

</div>

There is InvertedIndices.jl, which combined with `selectdim` might be the closest translation:

```julia
julia> using InvertedIndices: Not

julia> A = rand(Int8,4,5,3)
4×5×3 Array{Int8, 3}:
[:, :, 1] =
 -16 -71 64 -56 10
  42 -116 -111 106 -52
 -82 5 75 -75 -65
  89 -53 40 32 -74

[:, :, 2] =
   29 24 -77 21 -30
  -87 -38 64 40 51
   30 -84 44 -52 -29
 -118 55 -103 64 -97

[:, :, 3] =
 -109 52 32 100 -83
  -82 86 -24 117 121
  118 32 108 82 29
  124 106 -23 -87 62

julia> selectdim(A, 2, Not(2:4))
4×2×3 view(::Array{Int8, 3}, :, [1, 5], :) with eltype Int8:
[:, :, 1] =
 -16 10
  42 -52
 -82 -65
  89 -74

[:, :, 2] =
   29 -30
  -87 51
   30 -29
 -118 -97

[:, :, 3] =
 -109 -83
  -82 121
  118 29
  124 62

julia> ans == A[:, Not(2:4), :]
true

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 14, 2024, 5:29pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/4 "2024-11-14T17:29:20Z")

</div>

InvertedIndices.jl seems to underperform here?  
E.g:

```julia
using InvertedIndices: Not

A = rand(Int8,4,5,3)

B1 = A[:, ∉(2:4).(1:end), :]; # 54 ns (5 allocs: 192 bytes)
B2 = A[:, filter(!in(2:4), 1:end), :]; # 70 ns (6 allocs: 272 bytes)
B3 = selectdim(A, 2, Not(2:4)); # 324 ns (16 allos: 592 bytes)
B4 = A[:, Not(2:4), :]; # 856 ns (38 allocs: 1.36 KiB)

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 14, 2024, 5:47pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/5 "2024-11-14T17:47:49Z")

</div>

If your goal is simply to trim some number of slices from one edge (as in the initial example where you trim 2 slices from the beginning), the above can be further simplified to  
`a[:, :, begin+2:end]` with no need for `filter` or `InvertedIndices.jl`.

If you only want a single slice (for example, the last slice) from that dimension, you can write this as `a[:, :, end:end]`. If you’d prefer to drop that now-definitely-single dimension then `a[:, :, end]` will do that.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 14, 2024, 10:13pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/6 "2024-11-14T22:13:32Z")

</div>

> [@rafael.guerra](#):
>
> [InvertedIndices.jl](https://juliahub.com/ui/Packages/General/InvertedIndices) seems to underperform here?

It’s a common issue with InvertedIndices unfortunately, they are often surprisingly slow.

For removing indices in a vector, Accessors.jl are convenient and performant:

```julia
new_v = @delete v[123:456]

```

I think this only supports vectors for now – not because of some fundamental issue, just not implemented.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 14, 2024, 11:57pm UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/7 "2024-11-14T23:57:02Z")

</div>

> [@aplavin](#):
>
> I think this only supports vectors for now

Same situation as `deleteat!()`, which I think is more performant.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 15, 2024, 1:39am UTC](https://discourse.julialang.org/t/equivalent-of-np-delete/122642/8 "2024-11-15T01:39:04Z")

</div>

> [@rafael.guerra](#):
>
> `deleteat!()`, which I think is more performant

I think `@delete` should be the same performance as `copy + deleteat!`.
