# How do I skip an iteration of enumerator

**URL:** <https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 16, 2019, 4:30pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341 "2019-12-16T16:30:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [December 16, 2019, 4:30pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341/1 "2019-12-16T16:30:55Z")

</div>

Hi, I have the problem that I have an array which is changing during a loop and I have to skip certain elements.

What is the best way to do this?

```julia
a = [1,2,3]
itr = Iterators.Stateful(a)
for (i,el) in enumerate(itr)
    @show el
    if el == 2
        deleteat!(a,i)
        insert!(a,i,9)
        insert!(a,i,8)
        # remove extra iteration
        popfirst!(itr)
    end
end
a

# Output
el = 1
el = 2
el = 3

4-element Array{Int64,1}:
 1
 8
 9
 3

```

---

<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:** [December 16, 2019, 5:01pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341/2 "2019-12-16T17:01:52Z")

</div>

Generally `Iterators.filter` is useful for that, but in this particular case: I don’t think that modifying the underlying vector is allowed (“undefined consequences”).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 16, 2019, 5:04pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341/3 "2019-12-16T17:04:16Z")

</div>

Make a copy of the array to iterate over while you modify the other one? And/or just use a while loop if there’s a clear termination condition.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [December 16, 2019, 5:54pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341/4 "2019-12-16T17:54:32Z")

</div>

I’ll try to use a while loop. If length is calculated every iteration I think it should work.

```julia
i = 1
a=[1,2,3]
while i < length(a)

# change length and incr i

i += 1
end

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [December 16, 2019, 5:58pm UTC](https://discourse.julialang.org/t/how-do-i-skip-an-iteration-of-enumerator/32341/5 "2019-12-16T17:58:15Z")

</div>

Setting a flag to skip the next iteration combined with a `continue` could also work, I think.
