# The slowest zipper

**URL:** <https://discourse.julialang.org/t/the-slowest-zipper/56815>\
**Category:** General Usage\
**Created:** [March 9, 2021, 3:50pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815 "2021-03-09T15:50:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 9, 2021, 3:50pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/1 "2021-03-09T15:50:03Z")

</div>

I have an array of 1000 points, an `Array{Array{Float64,1}}` with length 1000. To separate the points into vectors of coordinates for each dimension, my Python instinct is to unpack the zipped points. For example, in Julia,

```julia
points = [rand(2) for i=1:1000]
x, y = zip(points...)

```

but this takes forever. I have to close the terminal window to kill it. Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [March 9, 2021, 4:01pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/2 "2021-03-09T16:01:58Z")

</div>

For xy vectors I usually do `xs = first.(points)` and `ys = last.(points)`. Or more generally `getindex.(points, Ref(1))`.

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 9, 2021, 4:08pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/3 "2021-03-09T16:08:30Z")

</div>

Ok, that works quickly, thank you. So, for an n-dimensional point the line would be  
`X = [getindex.(points, Ref(n)) for n in 1:length(points[1])]`  
or something similar?

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [March 9, 2021, 4:20pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/4 "2021-03-09T16:20:08Z")

</div>

I haven’t used the `getindex` version much, but I think that would work. You could also do something like

```julia
X = [[points[i][j] for i in eachindex(points)] for j in eachindex(points[1])]

```

if it doesn’t work or is slow.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 9, 2021, 5:30pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/5 "2021-03-09T17:30:51Z")

</div>

> [@markmbaum](#):
>
> but this takes forever. I have to close the terminal window to kill it. Am I doing something wrong?

The problem here is that the `...` operator in `zip(points...)` means you are calling the `zip` function with 1000 arguments (one for each point). That’s not something the Julia compiler is well-optimized for because it’s not something that people usually want. It takes forever because the compiler is (unhelpfully) trying to generate a specialized implementation for zipping 1000 different arguments.

By the way, if your actual use case involves storing thousands of arrays of some small and fixed size, [GitHub - JuliaArrays/StaticArrays.jl: Statically sized arrays for Julia](https://github.com/JuliaArrays/StaticArrays.jl) is a fantastic tool to make your code faster and your life easier.

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 9, 2021, 5:33pm UTC](https://discourse.julialang.org/t/the-slowest-zipper/56815/6 "2021-03-09T17:33:21Z")

</div>

Ok, thanks @rdeits, that makes sense 👍
