# List generation (array of arrays)

**URL:** <https://discourse.julialang.org/t/list-generation-array-of-arrays/64693>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [July 15, 2021, 2:14pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693 "2021-07-15T14:14:53Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 2:14pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/1 "2021-07-15T14:14:53Z")

</div>

Hello there,  
I was wondering how could we bulid a list like this:  
I have a array x = [1,2,3,4] and y = [5,6,7,8] and now I would like to come up a new array like  
z = [[1,5], [2,6], [3,7], [4,8] ] to reflects that these are coordinates. In my case I have 500 elements in x and y so it is impossible to do like above. And I tried several naive methods but all failed.  
Could anyone give some suggesstions to generate this kind of z list by more general method?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 15, 2021, 2:22pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/2 "2021-07-15T14:22:34Z")

</div>

```julia
julia> x = [1,2,3,4]; y = [5,6,7,8];

julia> z = [[x[i],y[i]] for i in 1:length(x) ]
4-element Vector{Vector{Int64}}:
 [1, 5]
 [2, 6]
 [3, 7]
 [4, 8]

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 15, 2021, 2:25pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/3 "2021-07-15T14:25:34Z")

</div>

Alternatively

```julia
julia> z = [[a, b] for (a, b) ∈ zip(x, y)]
4-element Vector{Vector{Int64}}:
 [1, 5]
 [2, 6]
 [3, 7]
 [4, 8]

```

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 2:27pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/4 "2021-07-15T14:27:56Z")

</div>

> [@lmiq](#):
>
> `z = [[x[i],y[i]] for i in 1:length(x) ]`

Ah! Indeed! Thank you for this kind, quick and helpful response!

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 2:29pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/5 "2021-07-15T14:29:44Z")

</div>

Thank you a lot ! !

---

<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:** [July 15, 2021, 2:29pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/6 "2021-07-15T14:29:51Z")

</div>

One more option:  
`xy = collect(eachrow([x y]))`

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 2:30pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/7 "2021-07-15T14:30:46Z")

</div>

Got it ! Thank you!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 15, 2021, 2:43pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/8 "2021-07-15T14:43:53Z")

</div>

> [@Cyber](#):
>
> I have a array x = [1,2,3,4] and y = [5,6,7,8] and now I would like to come up a new array like  
> z = [[1,5], [2,6], [3,7], [4,8] ] to reflects that these are coordinates.

For coordinate vectors, I would _strongly_ consider using [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) for performance reasons.

Then you would just use `SVector.(x,y)`

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 15, 2021, 2:49pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/9 "2021-07-15T14:49:01Z")

</div>

You can use `tuple.(x, y)` if you do not want external dependency and ok to work with tuples.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 15, 2021, 2:55pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/10 "2021-07-15T14:55:35Z")

</div>

Why not just

```julia
zip(x, y) 

```

?

Do you really need to turn them into vectors?

If you provide a bit more background on what you want to do, it would be possible to tell. Often you don’t _need_ a vector (or vector of vectors), just something to iterate over and do what you actually want to accomplish on each pair of coordinates.

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 3:09pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/11 "2021-07-15T15:09:07Z")

</div>

Thank you kindly all !  
Yes, I would like to simulate a 2D motion of some particles in a known domain and thay moves randomly. I have their initial positions and in the domain there are some holes which would absorb particles entered in. So I think I might need to store their coordinates in each time step and aslo need to determine whether to enter the holes.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 15, 2021, 3:18pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/12 "2021-07-15T15:18:09Z")

</div>

Ok, I can’t say that this is unnecessary, but think it over. If you can process the coordinate pairs, one after the other, you won’t need an intermediate array.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [July 15, 2021, 3:25pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/13 "2021-07-15T15:25:33Z")

</div>

Sorry, can’t help but share my favorite way for doing this: `collect.(zip(x,y))`. Then you can also try taking away the `collect.` at some point and see if you actually needed to collect.

---

<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:** [July 15, 2021, 3:34pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/14 "2021-07-15T15:34:24Z")

</div>

Or: `collect.(tuple.(x,y))`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 15, 2021, 4:08pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/15 "2021-07-15T16:08:30Z")

</div>

Then definitely you want StaticArrays. If it helps, [this package](https://github.com/m3g/FundamentosDMC.jl) is an educational package that does those kind of simulations of particles using that kind of structure. The codes are intended to be very simple and readable. [The tutorial](https://m3g.github.io/FundamentosDMC.jl/stable/) is in Spanish, but browser automatic translation tools are pretty good (the codes are in English).

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 4:13pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/16 "2021-07-15T16:13:33Z")

</div>

Got it! Going to check it out. Thank you again for your great help!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 15, 2021, 6:25pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/17 "2021-07-15T18:25:25Z")

</div>

> [@rafael.guerra](#):
>
> collect.(tuple.(x,y))

or just `tuple.(x, y)` 🙂

---

<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:** [July 15, 2021, 6:27pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/18 "2021-07-15T18:27:16Z")

</div>

> [@sijo](#):
>
> or just `tuple.(x, y)`

@Skoffer has already the copyright 🙂_(see further up…)_

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 15, 2021, 6:28pm UTC](https://discourse.julialang.org/t/list-generation-array-of-arrays/64693/19 "2021-07-15T18:28:08Z")

</div>

Oops I missed that, there, a well-deserved heart for @Skoffer🙂
