# Loop (i, j) without zip() stopped at the 2nd round

**URL:** <https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810>\
**Category:** General Usage\
**Tags:** for-loop\
**Created:** [August 16, 2022, 6:16am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810 "2022-08-16T06:16:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JiuuJulia](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Post date:** [August 16, 2022, 6:16am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/1 "2022-08-16T06:16:44Z")

</div>

I’m trying to loop two DNA sequences:

```julia
a = "AAAGCTAGCTAGCTAGACT"
b = "ATAGCTAGCCAGCTAAACT"

for (i, j) in (a,b)
    println(i, j)
end

```

It gives a result:

```julia
AA
AT

```

Why does the loop stop at the 2nd iteration?

I know I’d better do it with zip() as :

```julia
for (i, j) in zip(a,b)
    println(i, j)
end

```

and it gave the right answer.

```julia
AA
AT
AA
GG
CC
TT
AA
GG
CC
TC
AA
GG
CC
TT
AA
GA
AA
CC
TT

```

But still, I don’t understand why iteration stopped at the 2nd round without zip(), **and it didn’t raise any error!** Does anyone have any ideas? I can’t find the answer in the doc (or I didn’t go to the right place)

Julia version:

```julia
v"1.7.3"

```

Interesting approaches:

> [@rafael.guerra](#):
>
> `foreach(println, a, b)`

> [@Nathan\_Boyer](#):
>
> ```julia
> for i in eachindex(a)
> println(a[i], b[I])
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 16, 2022, 6:43am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/2 "2022-08-16T06:43:16Z")

</div>

> [@JiuuJulia](#):
>
> ```julia
> for (i, j) in (a,b)
> println(i, j)
> end
> 
> ```

You’re creating a tuple `(a,b)`, which is iterated in the `for` loop. So the first iteration will give you just `a`:

```julia
julia> iterate((a,b))
("AAAGCTAGCTAGCTAGACT", 2)

```

(The 2 is part of the iteration protocol, it’s the state of iteration).

The `a` is then destructured due to you writing `(i,j)` in the loop head:

```julia
julia> (i,j) = a
"AAAGCTAGCTAGCTAGACT"

julia> i
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)

julia> j
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)

julia> (i,j) = b
"ATAGCTAGCCAGCTAAACT"

julia> i
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)

julia> j
'T': ASCII/Unicode U+0054 (category Lu: Letter, uppercase)

```

As the loop over the `(a,b)` tuple is over, the loop terminates and only prints the first two characters of each string.

The loop you’ve written is equivalent to this one, which may be a bit clearer to understand why & how it works the way it does:

```julia
itr = (a,b)
_tmp = iterate(itr)
while _tmp !== nothing
    obj, state = _tmp
    # your loop body/specification starts
    i, j = obj
    println(i, j)
    # your loop body/specification ends
    _tmp = iterate(itr, state)
end

```

For more information, see [this](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) part of the docs about the iteration protocol.

---

<div class="post-metadata">

**Author:** ![JiuuJulia](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Post date:** [August 16, 2022, 7:55am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/4 "2022-08-16T07:55:31Z")

</div>

Thank you for your answer.  
I’m not sure if I completely understood this:

> [@Sukera](#):
>
> (The 2 is part of the iteration protocol, it’s the state of iteration).
> 
> The `a` is then destructured due to you writing `(i,j)` in the loop head:

Do you mean tuple `(i, j)` first points to the first two letters in a, and then points to the first two letters in b? (It took a while for me to make it clear in my mind 😂)

I wonder what “state” means here, does it mean the depth or the length of the tuple used for iteration? (e.g. in this case length of `(i,j)`is 2)

Cause when I create a long tuple, it worked.

```julia
a = "AAAGCTAGCTAGCTAGATC"
b = "ATAGCTAGCCAGCTAAACT"
c = "AGAGGATCGCAGCTATTGA"

str = collect(i for i in string.("i", 1:length(a)))

for (str) in (a,b,c)
    println(str)
end

```

Output:

```julia
AAAGCTAGCTAGCTAGATC
ATAGCTAGCCAGCTAAACT
AGAGGATCGCAGCTATTGA

```

😀

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 16, 2022, 8:04am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/5 "2022-08-16T08:04:15Z")

</div>

> [@JiuuJulia](#):
>
> Do you mean tuple `(i, j)` first points to the first two letters in a, and then points to the first two letters in b? (It took a while for me to make it clear in my mind 😂)

Yes, exactly.

> [@JiuuJulia](#):
>
> I wonder what “state” means here, does it mean the depth or the length of the tuple used for iteration? (e.g. in this case length of `(i,j)`is 2)

The iteration state is something that is passed to the next time `iterate` is called - it’s specific to each iterable. In the case of a tuple, it’s just the index of the next element in the tuple.

```julia
julia> a = "AAAGCTAGCTAGCTAGACT"
"AAAGCTAGCTAGCTAGACT"

julia> b = "ATAGCTAGCCAGCTAAACT"
"ATAGCTAGCCAGCTAAACT"

julia> tup = (a,b)
("AAAGCTAGCTAGCTAGACT", "ATAGCTAGCCAGCTAAACT")

julia> iterate(tup)
("AAAGCTAGCTAGCTAGACT", 2)

julia> iterate(tup, 2)
("ATAGCTAGCCAGCTAAACT", 3)

julia> iterate(tup, 3) # returns `nothing`, indicating the end of the iteration

```

The iteration state is arbitrary though, so it can be anything you’d like it to be for your own type.

> [@JiuuJulia](#):
>
> Cause when I create a long tuple, it worked.

Yes. You’re literally creating a tuple of three strings by writing `(a,b,c)`. Since loops have their own scope, your loop variable `str` shadows the existing `str` variable outside of the loop. So the `str` in your loop first is `a`, then `b`, then `c`.

---

<div class="post-metadata">

**Author:** ![JiuuJulia](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Post date:** [August 16, 2022, 8:23am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/6 "2022-08-16T08:23:51Z")

</div>

> [@Sukera](#):
>
> it’s just the index of the next element in the tuple

Ahh, no wonder the iteration while loop starts with `while _tmp !== nothing`

Thanks for your explanation.  
Julia is so neat.

---

<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:** [August 16, 2022, 8:44am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/7 "2022-08-16T08:44:51Z")

</div>

> [@JiuuJulia](#):
>
> Julia is so neat.

For your example, the following would be even neater:  
`foreach(println, a, b)`

---

<div class="post-metadata">

**Author:** ![JiuuJulia](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Post date:** [August 16, 2022, 1:55pm UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/8 "2022-08-16T13:55:04Z")

</div>

Didn’t know `foreach()`.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 16, 2022, 3:08pm UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/9 "2022-08-16T15:08:52Z")

</div>

`zip` is probably the best choice in this case, but here is another option:

```julia
julia> for i in eachindex(a)
           println(a[i], b[i])
       end
AA
AT
AA
GG
CC
TT
AA
GG
CC
TC
AA
GG
CC
TT
AA
GA
AA
CC
TT

```

You can read more about tuple destructuring [here](https://docs.julialang.org/en/v1/manual/functions/#destructuring-assignment).

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [August 16, 2022, 3:15pm UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/10 "2022-08-16T15:15:30Z")

</div>

I know it’s not relevant to your question, but if you work with DNA sequences, I recommend checking out `BioSequences` that contain relatively lightweight types specifically made for biological sequences like DNA.

---

<div class="post-metadata">

**Author:** ![JiuuJulia](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Post date:** [August 17, 2022, 1:33am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810/11 "2022-08-17T01:33:34Z")

</div>

Yes for sure! THANKS!
