# Why zip behaves differently in list comprehension?

**URL:** <https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 3, 2018, 6:07pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119 "2018-11-03T18:07:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirex/32/5910_2.png) [@sirex](https://discourse.julialang.org/u/sirex)\
**Post date:** [November 3, 2018, 6:07pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/1 "2018-11-03T18:07:31Z")

</div>

Here is what I got:

```julia
julia> for i in zip(1:3, 2:5)
           println(i)
       end
(1, 2)
(2, 3)
(3, 4)

julia> [i for i in zip(1:3, 2:5)]
ERROR: DimensionMismatch("dimensions must match")

```

Why `zip` works differently in list comprehension? Is this a feature or a bug?

---

<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:** [November 3, 2018, 6:52pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/2 "2018-11-03T18:52:42Z")

</div>

I’d say this is a bug because

```julia
julia> length(zip(1:3, 2:5))
3

julia> eltype(zip(1:3, 2:5))
Tuple{Int64,Int64}

```

makes pretty clear what collect should do.

---

<div class="post-metadata">

**Author:** ![sirex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirex/32/5910_2.png) [@sirex](https://discourse.julialang.org/u/sirex)\
**Post date:** [November 3, 2018, 7:16pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/3 "2018-11-03T19:16:14Z")

</div>

[https://github.com/JuliaLang/julia/issues/20499](https://github.com/JuliaLang/julia/issues/20499)

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [November 4, 2018, 2:11am UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/4 "2018-11-04T02:11:25Z")

</div>

The problem is that `zip(1:3, 1:4)` declares to have a well defined shape but errors when one asks what it is:

```julia
julia> itr = zip(1:3, 1:4)
Base.Iterators.Zip2{UnitRange{Int64},UnitRange{Int64}}(1:3, 1:4)

julia> Base.IteratorSize(itr)
Base.HasShape{1}()

julia> size(itr)
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at ./indices.jl:87 [inlined]
 [2] size(::Base.Iterators.Zip2{UnitRange{Int64},UnitRange{Int64}}) at ./iterators.jl:290
 [3] top-level scope at none:0

julia> length(itr)
3

```

Not sure what’s a good fix: the one dimensional case is easy, but the multidimensional case not so much:

```julia
collect(zip(rand(2, 3), rand(2, 3)))

```

For things to be type stable it should return a matrix. However what shape should the matrix have is a bit unclear, so maybe this needs to error.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 4, 2018, 6:22am UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/5 "2018-11-04T06:22:17Z")

</div>

This could be **utterly wrong** because I didn’t analyze impact to other areas!!! (and it is just **draft**!)

```julia
julia> import Base.axes

julia> axes(z::Base.Iterators.Zip2) = min(axes(z.a), axes(z.b))
axes (generic function with 29 methods)

julia> [i for i in zip(1:3,2:5)]
3-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (2, 3)
 (3, 4)

julia> collect(zip(rand(2, 3), rand(2, 3)))
2×3 Array{Tuple{Float64,Float64},2}:
 (0.67172, 0.578706) (0.745477, 0.868811) (0.759369, 0.390175)
 (0.786294, 0.0748731) (0.0990557, 0.746172) (0.160871, 0.711756)

```

I was inspired by:

```julia
length(z::Zip2) = _min_length(z.a, z.b, IteratorSize(z.a), IteratorSize(z.b))

```

which also means that draft above has to check (at least) infinite cases!

---

<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:** [November 4, 2018, 4:58pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/6 "2018-11-04T16:58:27Z")

</div>

[https://github.com/JuliaLang/julia/pull/29927](https://github.com/JuliaLang/julia/pull/29927)

---

<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:** [April 5, 2020, 7:18pm UTC](https://discourse.julialang.org/t/why-zip-behaves-differently-in-list-comprehension/17119/7 "2020-04-05T19:18:51Z")

</div>

Fixed on master
