# Eachindex for individual axes of multidimensional arrays?

**URL:** <https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264>\
**Category:** Internals & Design\
**Tags:** indexing, suggestions, arrays\
**Created:** [May 18, 2022, 2:26pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264 "2022-05-18T14:26:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjm](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@gjm](https://discourse.julialang.org/u/gjm)\
**Post date:** [May 18, 2022, 2:26pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/1 "2022-05-18T14:26:24Z")

</div>

Because arrays’ indices don’t generally start from 1, best practice is to avoid anything like “for i in 1:size()” and instead use eachindex. But for _multidimensional_ arrays, eachindex iterates over _all_ elements of the array, and sometimes you want to iterate only over some subset of its dimensions. (For instance: if you have a sequence of points in the plane, represented by a 2-dimensional array. Iterating over all the points means iterating over just one of the dimensions.)

If I understand correctly, the approved way to do this is something like

```julia
for i in firstindex(A,2):lastindex(A,2)
    x,y = A(1,i),A(2,i)
    # ... do something with x,y ...
end

```

but this is a bit cumbersome. (And when something is cumbersome, people will tend to do something less cumbersome instead even if it isn’t quite right. I bet there are a lot of loops of this sort that go from 1 to size(A,whatever). And all those loops are _wrong_ if it is possible for A to be an OffsetArray.)

I suggest that `eachindex` should take an optional second argument, a dimension number; obviously this should only succeed when the first argument is an `AbstractArray` rather than, say, a dictionary. So the loop above would become

```julia
for i in eachindex(A,2)
    x,y = A(1,i),A(2,i)
    # ... do something with x,y ...
end

```

`eachindex` can already take multiple arguments; they are all expected to be `AbstractArray`s of the same size and `eachindex` will return an indices object that works with them all. My proposal would give another meaning to calling `eachindex` with multiple arguments. Unless I am missing something there is no case in which it would give a new meaning to anything that was not previously illegal.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 18, 2022, 2:39pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/2 "2022-05-18T14:39:59Z")

</div>

> [@gjm](#):
>
> `for i in firstindex(A,2):lastindex(A,2)`

Iterate over the axes of the second dimension instead:

```julia
for i in axes(A, 2)

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 18, 2022, 2:55pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/3 "2022-05-18T14:55:51Z")

</div>

> [@gjm](#):
>
> Because arrays’ indices don’t generally start from 1, best practice is to avoid anything like “for i in 1:size()”

I know this is a hot topic these days but we should calm down on it. Julia **IS** a 1 based language and therefore **indices generally start from 1**. Cursing `1:length(whatever)` doesn’t look a good thing for me.

---

<div class="post-metadata">

**Author:** ![gjm](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@gjm](https://discourse.julialang.org/u/gjm)\
**Post date:** [May 18, 2022, 3:00pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/4 "2022-05-18T15:00:02Z")

</div>

Oh, good call. I somehow failed to see that, which I agree does everything I was after. May I suggest that the documentation for `eachindex` should maybe point at `axes` to make this easier to find?

---

<div class="post-metadata">

**Author:** ![gjm](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@gjm](https://discourse.julialang.org/u/gjm)\
**Post date:** [May 18, 2022, 3:02pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/5 "2022-05-18T15:02:12Z")

</div>

Sorry, my wording was suboptimal. “don’t generally” meant “don’t in full generality” or something, and it would have been better to say “don’t always”; of course I didn’t mean to say that indices don’t _usually_ start from 1.

But it is Just True that indices don’t _always_ start from 1, and using 1:length() is clearly worse than using eachindex.

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [May 18, 2022, 3:31pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/6 "2022-05-18T15:31:55Z")

</div>

> [@gjm](#):
>
> May I suggest that the documentation for `eachindex` should maybe point at `axes` to make this easier to find?

I echo this, axis has _ **See also: size, keys, eachindex** _.

eachindex should also include a _ **See also: axis, …** _

---

<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:** [May 18, 2022, 4:08pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/7 "2022-05-18T16:08:58Z")

</div>

See how this looks: [See also `axes` from `eachindex`'s docstring, etc. by mcabbott · Pull Request #45356 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/45356)

---

<div class="post-metadata">

**Author:** ![gjm](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@gjm](https://discourse.julialang.org/u/gjm)\
**Post date:** [May 18, 2022, 4:16pm UTC](https://discourse.julialang.org/t/eachindex-for-individual-axes-of-multidimensional-arrays/81264/8 "2022-05-18T16:16:12Z")

</div>

Looks good to me!
