# Enumerate loop: problem with indexing (method error)

**URL:** <https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168>\
**Category:** Performance\
**Created:** [November 5, 2018, 2:14pm UTC](https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168 "2018-11-05T14:14:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![axelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axelle/32/4488_2.png) [@axelle](https://discourse.julialang.org/u/axelle)\
**Post date:** [November 5, 2018, 2:14pm UTC](https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168/1 "2018-11-05T14:14:06Z")

</div>

Good afternoon,  
Using an enumerate loop, I get an error method when trying to fill in a vector with value calculated at each iteration of the loop. Would anyone know how to solve it? Many thanks for your help.

```julia
grid = collect(range(0.01, length=3, stop=1.0))   
  X= Vector  
for (i, D) in enumerate(grid)
    X[i] = i * D
 end

```

```julia
MethodError: no method matching setindex!(::Type{Array{T,1} where T}, ::Float64, ::Int32)

Stacktrace:
 [1] top-level scope at .\In[95]:4

```

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [November 5, 2018, 2:20pm UTC](https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168/2 "2018-11-05T14:20:05Z")

</div>

Your `X` is not a Vector but a `UnionAll`, since you assigned `X` to be the `type` Vector, not an instance of it as you could with `X = Vector()`.  
But that gives you an empty vector which you can’t access with `X[i]`, instead you can `push!(X, i*D)` to put `i*D` in `X`.  
Even better, you allocate a vector of the correct length with `X = Vector{Float64}(undef,3)` and then fill in the values just as you did with the loop. This generally gives the best performance since you don’t have to allocate a new vector within the loop, only _preallocate_ it once beforehand.

Cheers

---

<div class="post-metadata">

**Author:** ![axelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axelle/32/4488_2.png) [@axelle](https://discourse.julialang.org/u/axelle)\
**Post date:** [November 5, 2018, 2:22pm UTC](https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168/3 "2018-11-05T14:22:37Z")

</div>

Wonderful Peter. You solved it so fast. many many thanks. Also your explanation is useful to avoid this type of errors in the future. Have a nice day!

---

<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:** [November 5, 2018, 2:53pm UTC](https://discourse.julialang.org/t/enumerate-loop-problem-with-indexing-method-error/17168/4 "2018-11-05T14:53:56Z")

</div>

Firstly, there is no need to `collect` your range. In fact you should almost never do that, unless you have a very specific need. (For some reason, everyone wants to `collect` ranges all the time. Please don’t 😄 )

Secondly, here, I think a comprehension would be a very nice solution, and you don’t have to specify the type. Then your code becomes:

```julia
grid = range(0.01, length=3, stop=1.0) # don't collect here!
X = [i * D for (i, D) in enumerate(grid)]

```

You could also try:

```julia
X = eachindex(grid) .* grid

```
