# Any shortcut to 1:length(myVector)?

**URL:** https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816
**Category:** General Usage
**Created:** [January 4, 2021, 11:14am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816 "2021-01-04T11:14:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 4, 2021, 11:14am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/1 "2021-01-04T11:14:56Z")

</div>

Is there any build-in function in Julia to replace `1:length(myVector)`, i.e. to return a vector of integers from 1 to the length of `myVector` ?

e.g.:

```julia
myVector =["a","b","c"]
for i in ???(myVector)
    println(i) # 1,2,3
end

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 4, 2021, 11:17am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/2 "2021-01-04T11:17:54Z")

</div>

There is [`eachindex`](https://docs.julialang.org/en/v1/base/arrays/#Base.eachindex).

```julia
julia> myVec = ["a","b","c"];

julia> for i in eachindex(myVec)
           println(i)
       end
1
2
3

```

Note, however, that this doesn’t necessarily give integers but whatever is appropriate/fast to index `myVec` with.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 4, 2021, 12:02pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/3 "2021-01-04T12:02:14Z")

</div>

`1:length` is pretty short, just 8 characters. Also, it isn’t normally what you should be using, not for indexing, at least.

In a way, `1:length` is a bit of an ad-hoc pattern, which probably _shouldn’t_ have it’s own function, imho.

---

<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: [January 4, 2021, 12:02pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/4 "2021-01-04T12:02:37Z")

</div>

There is also

```julia
for i in firstindex(x):lastindex(x)
   println(i)
end

```

which has the advantage of supporting offset arrays.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 4, 2021, 12:04pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/5 "2021-01-04T12:04:07Z")

</div>

Is this ever different from `eachindex`?

---

<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: [January 4, 2021, 12:10pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/6 "2021-01-04T12:10:46Z")

</div>

Apparently yes, it always returns integers (example adapted from [here](https://docs.julialang.org/en/v1/base/arrays/#Base.eachindex)):

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

julia> B = view(A,1:2,1:1)
2×1 view(::Array{Int64,2}, 1:2, 1:1) with eltype Int64:
 1
 2

julia> for i in eachindex(B)
         println(i)
       end
CartesianIndex(1, 1)
CartesianIndex(2, 1)

julia> for i in firstindex(B):lastindex(B)
         println(i)
       end
1
2

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 4, 2021, 12:15pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/7 "2021-01-04T12:15:30Z")

</div>

Maybe `LinearIndices`, then:

```julia
for i in LinearIndices(B)
    println(i)
end

1
2

```

---

<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 4, 2021, 3:34pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/8 "2021-01-04T15:34:00Z")

</div>

@leandromartinez98, for offset arrays, don’t we have to use `pairs` to obtain the offset indices? Example:

```julia
using OffsetArrays
A = [1 2 3; 4 5 6];
B = OffsetArray(A,-1:0,0:2)
for (i,) in pairs(B)
    println((i[1],i[2]))
end
(-1, 0)
(0, 0)
(-1, 1)
(0, 1)
(-1, 2)
(0, 2)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 4, 2021, 3:57pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/9 "2021-01-04T15:57:52Z")

</div>

Afaik `getindex(::OffsetArray, ::Int)` works fine, it’s only `OffsetVectors` for which integer indexes are ambiguous. `eachindex` should always work, because it takes care of this, ie

```julia
julia> A = OffsetArray(1:2, 3:4)
1:2 with indices 3:4

julia> B = OffsetArray(reshape(1:6, 2, 3), -1:0, 0:2)
2×3 OffsetArray(reshape(::UnitRange{Int64}, 2, 3), -1:0, 0:2) with eltype Int64 with indices -1:0×0:2:
 1 3 5
 2 4 6

julia> eachindex(A)
OffsetArrays.IdOffsetRange(3:4)

julia> eachindex(B)
Base.OneTo(6)

```

---

<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: [January 4, 2021, 4:17pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/10 "2021-01-04T16:17:13Z")

</div>

> [@rafael.guerra](#):
>
> don’t we have to use `pairs` to obtain the offset indices?

I think things are getting mixed up here. But the OP wanted a way to obtain a vector of integers of the indexes of an array (I guess one-dimensional). `firstindex` and `lastindex` will do the job for offset arrays (as eachindex, but this one may return something that is not the indexes exactly, I am not sure how often and which are the collections for which that does not happen).

```julia
julia> using OffsetArrays

julia> x = OffsetArray([1, 2, 3],0:2)
3-element OffsetArray(::Array{Int64,1}, 0:2) with eltype Int64 with indices 0:2:
 1
 2
 3

julia> for i in firstindex(x):lastindex(x)
         println(i)
       end
0
1
2

```

By the way, although I think the OP knows that, the original question was how to obtain _a vector_ of the indexes of an array.

Perhaps just for the sake of completeness:

```julia
julia> x = ["a","b","c"]
3-element Array{String,1}:
 "a"
 "b"
 "c"

julia> indexes = collect(eachindex(x))
3-element Array{Int64,1}:
 1
 2
 3

```

One thing that motivated my original mention of `firstindex` and `lastindex` was that I was not sure that `eachindex` necessarily returned the range _in order_. (the notation _eachindex_ perhaps suggests that it does not, and that the iterations could occur out of order if that resulted to be more efficient for some reason. Apparently there is such guarantee?

---

<div class="post-metadata">

### Author: ![David\_Ortley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_ortley/32/7845_2.png) [@David\_Ortley](https://discourse.julialang.org/u/David_Ortley)
#### Post date: [January 4, 2021, 5:14pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/11 "2021-01-04T17:14:42Z")

</div>

There’s also `enumerate()`, which returns a tuple containing your incrementing index and the the value from `myVector` at that index. Useful if you’re wanting to both go through myVector and get the index for other reasons at the same time.

Note how I had to unpack the tuple in the for-loop with the `()`'s around `i` and `x`.

```julia
myVector =["a","b","c"]
for (i, x) in enumerate(myVector)
    println("$i: $x")
end

# output:
# 1: a
# 2: b
# 3: c

```

---

<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: [January 4, 2021, 5:56pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/12 "2021-01-04T17:56:18Z")

</div>

> [@David\_Ortley](#):
>
> There’s also `enumerate()` , which returns a tuple containing your incrementing index and the the value from `myVector` at that index.

I would like to note that `enumerate` documentation says:

> An iterator that yields `(i, x)` where `i` is a counter starting at 1, and `x` is the ith value from the given iterator. It’s useful when you need not only the values `x` over which you are iterating, but also the number of iterations so far. Note that `i` may not be valid for indexing `iter`; it’s also possible that `x != iter[i]`, if `iter` has indices that do not start at `1`. See the `pairs(IndexLinear(), iter)` method if you want to ensure that `i` is an index.

So it has nothing to do with indexes, and it will give you incorrect index values if you are using any “non-vanilla” array. It is just a convenience for the cases in which you wanted to have a counter being incremented at each iteration.

---

<div class="post-metadata">

### Author: ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)
#### Post date: [January 4, 2021, 6:19pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/13 "2021-01-04T18:19:34Z")

</div>

This is slightly off topic but I have often used `for (index,value) in enumerate(X)` to create a loop where I need both the index and the value to work with. This has been fine for default arrays and I haven’t had an issue. It would be neat eventually to have a version that worked with arbitrarily indexed arrays by returning the value and the same indices returned by `eachindex(X)`

At the same time I’ve been reducing my usage of this pattern using broadcast and higher-ordered functions.

---

<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: [January 4, 2021, 6:21pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/14 "2021-01-04T18:21:12Z")

</div>

> [@Jordan\_Cluts](#):
>
> It would be neat eventually to have a version that worked with arbitrarily indexed arrays by returning the value and the same indices returned by `eachindex(X)`

That’s [`pairs`](https://docs.julialang.org/en/v1/base/collections/#Base.pairs) as noted above.

---

<div class="post-metadata">

### Author: ![David\_Ortley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_ortley/32/7845_2.png) [@David\_Ortley](https://discourse.julialang.org/u/David_Ortley)
#### Post date: [January 4, 2021, 6:46pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/16 "2021-01-04T18:46:11Z")

</div>

Thanks for the clarification. Hadn’t thought about that aspect of it.

(sorry, this was a reply to Henrique\_Becker’s comment on my post.)

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [January 4, 2021, 10:01pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/17 "2021-01-04T22:01:32Z")

</div>

My fav (not yet mentioned) `for i in axes(myVector,1)`

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 4, 2021, 10:24pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/18 "2021-01-04T22:24:58Z")

</div>

Since functions are first class citizens in Julia, you can always create yours:

```julia
julia> inds(x) = firstindex(x):lastindex(x) |> collect;

julia> myVector =["a","b","c"];

julia> inds(myVector)
3-element Array{Int64,1}:
 1
 2
 3

```

---

<div class="post-metadata">

### Author: ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)
#### Post date: [January 5, 2021, 4:44pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/19 "2021-01-05T16:44:06Z")

</div>

> [@mbauman](#):
>
> That’s [`pairs`](https://docs.julialang.org/en/v1/base/collections/#Base.pairs) as noted above.

Mind blown, I knew about `pairs` but only had it in my mind in the context of Dictionaries. Using it for arrays is awesome!

---

<div class="post-metadata">

### Author: ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)
#### Post date: [January 8, 2021, 8:01am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/20 "2021-01-08T08:01:42Z")

</div>

I recently started to use `keys` for this:

```julia
for i in keys(myVector)
    ...
end

```

The added benefit is there is no need to touch that even if I change the underlying container type.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 8, 2021, 9:42am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/21 "2021-01-08T09:42:19Z")

</div>

Note that if you don’t just need the indexes, but are lookup up the corresponding element (`myVector[i]`), `pairs` may be efficient for some collections.

[Next page](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816.md?page=2)
