# Whats a better way to remove elements from an array based on index?\`\`\`g = \[55, 7

**URL:** <https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340>\
**Category:** General Usage\
**Created:** [March 2, 2021, 3:28pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340 "2021-03-02T15:28:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 2, 2021, 3:28pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/1 "2021-03-02T15:28:23Z")

</div>

whats a better way to remove elements from an array based on index?

```julia
g = [55, 77, 88, 99, 3, 7, 8,99,99, 999]
ix_drop = 2:4
setdiff(g, g[ix_drop])

```

This works but it will remove duplicate values… and thats less than desirable…

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614698880207000?thread_ts=1614698880.207000&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 2, 2021, 4:18pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/2 "2021-03-02T16:18:54Z")

</div>

> [@BridgeBot](#):
>
> ```julia
> g = [55, 77, 88, 99, 3, 7, 8,99,99, 999]
> ix_drop = 2:4
> 
> ```

```julia
deleteat!(g,ix_drop)

```

is the best solution (pointed on the slack thread, I am confused on now this works), but if one does not want to modify the original vector, but get a new one, one can do, for example:

```julia
julia> g[eachindex(g) .∉ Ref(ix_drop)]
7-element Array{Int64,1}:
  55
   3
   7
   8
  99
  99
 999

```

---

<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:** [March 2, 2021, 6:51pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/3 "2021-03-02T18:51:15Z")

</div>

`deletat!` works fine on Julia Version 1.6.0-beta1.0:

```julia
g = [55, 77, 88, 99, 3, 7, 8,99,99, 999]
ix_drop = 2:4
deleteat!(g,ix_drop);
g
7-element Vector{Int64}:
  55
   3
   7
   8
  99
  99
 999

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 2, 2021, 6:59pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/4 "2021-03-02T18:59:30Z")

</div>

It does. It is just that it overwrites the original vector. That may or may not be wanted.

---

<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:** [March 2, 2021, 7:13pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/5 "2021-03-02T19:13:04Z")

</div>

Sorry for misreading your post.  
Another way in that case is to use `setdiff`:

```julia
g[setdiff(1:length(g), ix_drop)]

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 2, 2021, 7:16pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/6 "2021-03-02T19:16:00Z")

</div>

That is probably faster.

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [March 2, 2021, 7:24pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/7 "2021-03-02T19:24:23Z")

</div>

or just

```julia
g[setdiff(1:end, ix_drop)]

```

or even

```julia
g[setdiff(begin:end, ix_drop)]

```

---

<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:** [March 2, 2021, 7:29pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/8 "2021-03-02T19:29:52Z")

</div>

This one looks nice too:

```julia
g[(begin:end) .∉ (ix_drop,)]

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 2, 2021, 7:39pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/9 "2021-03-02T19:39:50Z")

</div>

> [@haberdashPI](#):
>
> `g[setdiff(begin:end, ix_drop)]`

Interesting, although my reaction is `begin` and `end` of what?? Didn’t know that those keywords worked like that.

> [@rafael.guerra](#):
>
> `(ix_drop,)]`

That comma breaks the magic… I prefer `g[begin:end .∉ Ref(ix_drop)]`  
This one could go the the thread of syntatic sugars, if not because the `setdiff` is the fastest option.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 2, 2021, 9:16pm UTC](https://discourse.julialang.org/t/whats-a-better-way-to-remove-elements-from-an-array-based-on-index-g-55-7/56340/10 "2021-03-02T21:16:00Z")

</div>

One thing I did not found in `Base` and implemented myself is the case in which you do not want to change the original vector, and you also want two new arrays, one for which the elements return `true` for some predicate, and the complement (i.e., the array for the values that evaluated `false`). Using two `filter` seemed terribly wasteful for me, it is a thing that theoretically needs a single sweep. So I wrote a [small function that does that](https://github.com/henriquebecker91/GuillotineModels.jl/blob/cee802396980a9dd3bda07278ba348bafbb61c3b/src/PPG2KP/PPG2KP.jl#L94).
