# Zip with length checking

**URL:** <https://discourse.julialang.org/t/zip-with-length-checking/16395>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 16, 2018, 11:28am UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395 "2018-10-16T11:28:29Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [October 16, 2018, 11:28am UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/1 "2018-10-16T11:28:29Z")

</div>

Consider a loop

```julia
for (i, elt) in zip(indexes, itr)
    do_something(i, elt)
end

```

where `Base.IteratorSize(itr)` can potentially be `Base.HasLength()` or `Base.SizeUnknown()`. I want the code to work for both.

`indexes` is an `AbstractVector` so I know its `length`.

I want to check that the iteration does not terminate “early” because `itr` is shorter than `indexes. What’s the idiomatic way to do so?

I thought of the following:

1. manually `iterate` itr using,
2. use a counter and check that.

But neither seems elegant.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 16, 2018, 11:44am UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/2 "2018-10-16T11:44:18Z")

</div>

`zip(indices, takestrict(itr, length(indices)))` from `Itertools` would be a choice. Actually, I might add `zipstrict` to `Itertools`.

---

<div class="post-metadata">

**Author:** ![RGonTheNoble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgonthenoble/32/209422_2.png) [@RGonTheNoble](https://discourse.julialang.org/u/RGonTheNoble)\
**Post date:** [July 23, 2025, 3:23pm UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/3 "2025-07-23T15:23:34Z")

</div>

Sorry to resurrect this thread, but has this actually been implemented in IteratorTools or base iterators? `zipstrict` seems like it would be a very helpful function to do “map” with multiple arguments in a way that’s similar to broadcast.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [July 23, 2025, 3:43pm UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/4 "2025-07-23T15:43:00Z")

</div>

there is `Iterators._zip_lengths_finite_equal` which is undocumented & internal, but I think in this case it’s pretty fine to just use it anyway

it might be reasonable to define

```julia
zipstrict(a...) = _zip_lengths_finite_equal(a) ? throw(ArgumentError(...)) : Zip(a)

```

or use a `strict` kwarg. but there isn’t really precedence for this kind of API in any existing iterators in `Base.Iterators`

---

<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:** [July 23, 2025, 3:58pm UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/5 "2025-07-23T15:58:13Z")

</div>

That won’t work, unfortunately, with `SizeUnknown`s. For that you do need to reach into the zip implementation to demand that running out of one iterator asserts that all iterators are complete.

---

<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:** [July 24, 2025, 8:12am UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/6 "2025-07-24T08:12:08Z")

</div>

> [@RGonTheNoble](#):
>
> has this actually been implemented in IteratorTools or base iterators?

AFAIK, no. I think it would be a fine addition to IterTools.jl.

The implementation would have to consider all the size trait combinations mentioned in this topic, also for more than two arguments, but in principle this should not be difficult.

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [July 24, 2025, 11:25pm UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/7 "2025-07-24T23:25:31Z")

</div>

Which one would be preferable?

- `lazy`: as soon as one of the iterators runs out, raise an exception if there were any iterators before it in the ziplist that didn’t run out, or any iterators after it that don’t run out
- `eager`: raise an exception if we can’t validate ahead of time that all iterators are either infinite or the same length, and then just let a normal `zip` happen

The lazy version seems pretty easy to implement in a way that covers all cases, right? And the eager version is impossible to get to work with `SizeUnknown`s, as @mbauman points out?

---

<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:** [July 27, 2025, 2:44pm UTC](https://discourse.julialang.org/t/zip-with-length-checking/16395/8 "2025-07-27T14:44:35Z")

</div>

I would go with lazy first, and also as a fallback, and then specialize to eager for the cases where this leads to a significant improvement.
