# \`collect\` gives different result than \`for\` loop

**URL:** <https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509>\
**Category:** General Usage\
**Tags:** question, iterators\
**Created:** [May 4, 2022, 8:58pm UTC](https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509 "2022-05-04T20:58:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MillironX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millironx/32/28274_2.png) [@MillironX](https://discourse.julialang.org/u/MillironX)\
**Post date:** [May 4, 2022, 8:58pm UTC](https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509/1 "2022-05-04T20:58:13Z")

</div>

I’ve somehow created an iterator function where calling `collect` on the `Alignment` struct returns a 5-element vector, while iterating produces only three elements.

Context: I’m working on a patch for BioAlignments ([https://github.com/MillironX/BioAlignments.jl/tree/operations-fix](https://github.com/MillironX/BioAlignments.jl/tree/operations-fix)), where I modified the `Base.iterate` function, but I don’t know how to debug this since stepping into the iterator doesn’t causes the input to become correct again. Does anyone know how this could happen?

Iterator code: [https://github.com/MillironX/BioAlignments.jl/blob/5a8cb6bb3179e00adf7f750e26952257ca70f810/src/pairwise/alignment.jl#L17-L73](https://github.com/MillironX/BioAlignments.jl/blob/5a8cb6bb3179e00adf7f750e26952257ca70f810/src/pairwise/alignment.jl#L17-L73)

MWE:

```julia
using BioAlignments

anchors = [
    AlignmentAnchor(0, 0, 0, OP_START),
    AlignmentAnchor(2, 2, 2, OP_SEQ_MATCH),
    AlignmentAnchor(2, 2, 3, OP_PAD),
    AlignmentAnchor(3, 3, 4, OP_SEQ_MATCH),
    AlignmentAnchor(3, 3, 5, OP_HARD_CLIP),
]
seq = AlignedSequence("ACG", anchors)
ref = "ACG"
aln = PairwiseAlignment(seq, ref)

@show collect(aln)

for (k, (i,j)) in enumerate(aln)
    @show k
    @show i
    @show j
    println(' ')
end

```

Output:

```julia
collect(aln) = [('A', 'A'), ('C', 'C'), ('G', 'G'), ('\0', '\0'), ('\0', '\0')]
k = 1
i = 'A'
j = 'A'
 
k = 2
i = 'C'
j = 'C'
 
k = 3
i = 'G'
j = 'G'

```

---

<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:** [May 4, 2022, 9:22pm UTC](https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509/2 "2022-05-04T21:22:27Z")

</div>

What is `length(aln)`? I’m guessing your discrepancy is there.

---

<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:** [May 4, 2022, 9:27pm UTC](https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509/3 "2022-05-04T21:27:26Z")

</div>

I’ve seen such behavior a few times. It usually happens when `length` claims one length, but actually iterating produces fewer elements. `collect` preallocates based on `length`, but doesn’t shrink to the number of elements actually produced (counting them may be slow).

Would be interesting if we could add a `LengthBounded()` trait to opt into that allows shrinking the resulting collection if fewer than `length(itr)` elements were actually produced.

---

<div class="post-metadata">

**Author:** ![MillironX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millironx/32/28274_2.png) [@MillironX](https://discourse.julialang.org/u/MillironX)\
**Post date:** [May 4, 2022, 9:29pm UTC](https://discourse.julialang.org/t/collect-gives-different-result-than-for-loop/80509/4 "2022-05-04T21:29:29Z")

</div>

```julia
julia> length(aln)
5

```
