# Strange behavior with list comprehensions using Iterators.Stateful

**URL:** https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715
**Category:** General Usage
**Created:** [October 10, 2019, 6:02am UTC](https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715 "2019-10-10T06:02:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![altre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/altre/32/17036_2.png) [@altre](https://discourse.julialang.org/u/altre)
#### Post date: [October 10, 2019, 6:02am UTC](https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715/1 "2019-10-10T06:02:51Z")

</div>

I’m having trouble to understand the following behavior of Iterators.Stateful. This is as expected

```julia
julia> for a in Iterators.Stateful([1,2,3])
             print(a)
         end
123

```

But this seems strange:

```julia
julia> [a for a in Iterators.Stateful([1,2,3])]
2-element Array{Int64,1}:
 1
 2
julia> [a for a in Iterators.Stateful([1])]
ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [1]

```

Is this a bug, or is there something I don’t understand here?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 10, 2019, 7:58am UTC](https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715/2 "2019-10-10T07:58:19Z")

</div>

Looking at the code

[https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L630-L643](https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L630-L643)

The `iterate` call at

[https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L636](https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L636)

makes `length(itr)` go from 1 to 0.

Then in `_array_for(typeof(v1), itr.iter, isz)`:

[https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L627](https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L627)

a length `0` vector is allocated which is then tried to get assigned into:

[https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L657-L661](https://github.com/JuliaLang/julia/blob/09795cdd2e1d1af291d87122897a580c78352999/base/array.jl#L657-L661)

I’m guessing the `axis` / `length` should be computed before the call to `iterate`.

---

<div class="post-metadata">

### Author: ![racinmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/racinmat/32/11715_2.png) [@racinmat](https://discourse.julialang.org/u/racinmat)
#### Post date: [April 1, 2020, 7:54pm UTC](https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715/3 "2020-04-01T19:54:46Z")

</div>

That quite looks like a bug, is there issue for it on github?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 1, 2020, 8:47pm UTC](https://discourse.julialang.org/t/strange-behavior-with-list-comprehensions-using-iterators-stateful/29715/4 "2020-04-01T20:47:08Z")

</div>

> [@racinmat](#):
>
> is there issue for it on github?

Don’t think so.
