# Selecting multiple elements of an array by a list of indices

**URL:** https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483
**Category:** New to Julia
**Tags:** indexing, arrays
**Created:** [September 29, 2020, 1:20pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483 "2020-09-29T13:20:14Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)
#### Post date: [September 29, 2020, 1:20pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/1 "2020-09-29T13:20:14Z")

</div>

I have a list of indices, say `inds = [[1,2], [2,3],[4,4]]` that I want to use as indices into an array. For example, if

`A = reshape(1:16,4,4)`

then I would want to return the values `[5, 10, 16]`. I can, for example, use:

`[A[x[1],x[2]] for x in inds]`

but that seems very clumsy; I would want something more elegant, like `A[inds]` (which doesn’t work). What is the canonical Julia way of pulling a list of values from an array, using a list of indices to do so? And where can I find this in the documentation? It seems as though it would be a pretty standard sort of operation, but clearly I’ve either been looking in the wrong places, or misunderstanding what I’ve been reading… Anyway, thanks!

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [September 29, 2020, 1:25pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/2 "2020-09-29T13:25:51Z")

</div>

A little bit less clumsy:

```julia
[A[x,y] for (x,y) in inds]

```

---

<div class="post-metadata">

### Author: ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)
#### Post date: [September 29, 2020, 1:27pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/3 "2020-09-29T13:27:07Z")

</div>

Thank you - yes indeed that is more elegant. But can it be done without having to explicitly reference both indices in each pair?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [September 29, 2020, 1:38pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/4 "2020-09-29T13:38:55Z")

</div>

Not sure, but I think no, because

```julia
 getindex(A, [2, 1])

```

is already defined as returning `A[2]` and `A[1`] and not `A[1,2]`.

`A` is an `AbstractArray` in above case, so you could implement `getindex` for `AbstractMatrix`.  
See

```julia
julia> methods(getindex)

```

---

<div class="post-metadata">

### Author: ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)
#### Post date: [September 29, 2020, 1:45pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/5 "2020-09-29T13:45:15Z")

</div>

Thanks again - yes, I’ve been trying all sorts of combinations, but they return either a subarray, or an error. But I’ll check on `getindex`. Of course, given that I know the size of my array, I could create my indices as single values `i+n*(j-1)` for an n by n array. That would give me `inds = [5,10,16]` and then `A[inds]` works fine. In other words, using linear indexing instead of cartesian indexing.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [September 29, 2020, 1:47pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/6 "2020-09-29T13:47:19Z")

</div>

Quick and Dirty and only for Int:

```julia
julia> import Base.getindex

julia> getindex(A::AbstractMatrix,i::AbstractArray{Int,1}) = A[i[1],i[2]]
getindex (generic function with 199 methods)

julia> A[[1,2]]
5

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [September 29, 2020, 2:04pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/7 "2020-09-29T14:04:47Z")

</div>

> [@amca01](#):
>
> But can it be done without having to explicitly reference both indices in each pair?

A bit weird maybe but:

```julia
[A[i...] for i in inds]

```

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [September 29, 2020, 2:09pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/8 "2020-09-29T14:09:50Z")

</div>

It can be done. `CartesianIndices` are your _best_ friends when it comes to indexing.

```julia
julia> inds = [[1,2], [2,3],[4,4]];

julia> A = reshape(1:16,4,4);

julia> A[CartesianIndex.(Tuple.(inds))]
3-element Array{Int64,1}:
  5
 10
 16

```

---

<div class="post-metadata">

### Author: ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)
#### Post date: [September 29, 2020, 2:14pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/9 "2020-09-29T14:14:45Z")

</div>

Oh thank you! I had a vague inkling that `CartesianIndex` might be able to help, but I couldn’t see how to do it - so it’s all done with broadcasting! Very nice indeed, and again, many thanks.

---

<div class="post-metadata">

### Author: ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)
#### Post date: [September 29, 2020, 2:16pm UTC](https://discourse.julialang.org/t/selecting-multiple-elements-of-an-array-by-a-list-of-indices/47483/10 "2020-09-29T14:16:38Z")

</div>

Thank you - clearly the triple dots hold some sort of magic of which I’m unaware. It does work though! (I’ve just discovered that this is in fact the superbly named _splat_ operator.)
