# Easiest way to delete indices from Vector{Vector{}}

**URL:** <https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734>\
**Category:** New to Julia\
**Tags:** arrays, vector\
**Created:** [January 17, 2022, 6:44am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734 "2022-01-17T06:44:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 17, 2022, 6:44am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/1 "2022-01-17T06:44:38Z")

</div>

How to i obtain following

Input:  
indices to delete

```julia
ind = [2,4]

```

Vector of Vector

```julia
a = [[1.0,2,3,4,6,2], [10.0, 13, 4, 5, 2, 8] ]

```

Expected Output as below

```julia
2-element Vector{Vector{Float64}}:
 [1.0, 3.0, 6.0, 2.0]
 [10.0, 4.0, 2.0, 8.0]

```

For now, my implementation is by following way:

```julia
julia> [a[i][setdiff(1:length(a[i]), ind)] for i in 1:length(a)]
2-element Vector{Vector{Float64}}:
 [1.0, 3.0, 6.0, 2.0]
 [10.0, 4.0, 2.0, 8.0]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 17, 2022, 7:02am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/2 "2022-01-17T07:02:21Z")

</div>

If mutation is fine

```julia
deleteat!.(a, Ref(ind))

```

otherwise

```julia
deleteat!.(copy.(a), Ref(ind))

```

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 17, 2022, 7:08am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/3 "2022-01-17T07:08:41Z")

</div>

Thanks ! That seems elegant 😀

---

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [January 17, 2022, 7:41am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/4 "2022-01-17T07:41:17Z")

</div>

Without the `Ref()` it appears that only a single element is removed from each element of `a` (and an error is raised if `ind` and `a` have different `length`s). Why is the behavior different?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 17, 2022, 8:24am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/5 "2022-01-17T08:24:20Z")

</div>

See [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting) for documentation of broadcasting. Let’s consider a few simple cases of relevance here (and somewhat oversimplify how it works):

If `f` is a function, `a` a vector, and `b` a scalar, then the dot syntax

```julia
f.(a, b)

```

means

```julia
[f(a[i], b) for i in 1:length(a)]

```

If ‘b’ is instead a vector of length 1 it means

```julia
[f(a[i], b[1]) for i in 1:length(a)]

```

If `b` is a vector of length more than one it is indexed together with ‘a’

```julia
[f(a[i], b[i]) for i in 1:length(a)]

```

assuming that `a` and `b` have the same length, otherwise it’s an error.

For our `deleteat!` application we don’t want to index into ‘b’, which we can avoid by placing it in a container of size 1, e.g. `[b]` (single element vector) or `(b,)` (single element tuple)

```julia
f.(a, (b,))

```

which means

```julia
[f(a[i], (b,)[1]) for i in 1:length(a)]

```

or equivalently

```julia
[f(a[i], b) for i in 1:length(a)]

```

The use of `Ref(b)` is equivalent to `(b,)` in this context.

---

<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:** [January 17, 2022, 8:43am UTC](https://discourse.julialang.org/t/easiest-way-to-delete-indices-from-vector-vector/74734/6 "2022-01-17T08:43:42Z")

</div>

> [@GunnarFarneback](#):
>
> `deleteat!.(a, Ref(ind))`

Just to note that in Julia, `for-loop` alternatives “never” hurt. Perhaps:

```julia
for v in a; deleteat!(v, ind); end

```
