# Q: Vector of vectors of variable lengths

**URL:** <https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817>\
**Category:** General Usage\
**Tags:** question, arrays, vector-of-vectors\
**Created:** [August 15, 2023, 8:21am UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817 "2023-08-15T08:21:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [August 15, 2023, 8:21am UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/1 "2023-08-15T08:21:15Z")

</div>

Hello,

What is the recommended/best Julia method to create a vector of vectors of variable lengths?

Current method MWE:

```julia
# test_vector_of_vectors.jl

begin
    vec_of_vecs = [Float64[] for _ in 1:3]
    for i = 1:3
        push!(vec_of_vecs[1], 0.0)
    end
    for i = 1:5
        push!(vec_of_vecs[2], 1.0)
    end
    for i = 1:7
        push!(vec_of_vecs[3], 2.0)
    end

    @show vec_of_vecs
end

```

[In the actual code, the variable length vector are workspace arrays, , all initialized to 0.0, to all be passed to a set of functions.]

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 15, 2023, 8:34am UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/2 "2023-08-15T08:34:45Z")

</div>

I bit more terse:

```julia
vec_of_vecs = [zeros(Float64,s*2+1) for s in 1:3]

```

If the individual sizes can be calculated from the index. If not I would do it more like:

```julia
sizes=[7, 3, 5]
vec_of_vecs = [zeros(Float64,sizes[s]) for s in 1:3]

```

Don’t know if this is recommended (by whom) or the best (in what sense). If it is important to have the best performance, because it’s perhaps in a hot loop, my guess is, complete performance depends more on the whole thing you want to do, the context.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 15, 2023, 9:02am UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/3 "2023-08-15T09:02:40Z")

</div>

Or, to set the values too:

```julia
vec_of_vecs = [fill( Float64(s), s*2+1 ) for s in 1:3]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [August 15, 2023, 9:11am UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/4 "2023-08-15T09:11:51Z")

</div>

> [@oheil](#):
>
> `vec_of_vecs = [zeros(Float64,sizes[s]) for s in 1:3]`

Or slightly more succinct:

```julia-auto
vec_of_vecs = [zeros(Float64, s) for s in sizes]

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [August 15, 2023, 1:06pm UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/5 "2023-08-15T13:06:31Z")

</div>

Could even use broadcasting:

```julia
vec_of_vecs = zeros.(s) # default type is Float64

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 15, 2023, 1:15pm UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/6 "2023-08-15T13:15:32Z")

</div>

Should be in the flow of examples:

```julia
vec_of_vecs = zeros.(sizes)

```

---

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [August 15, 2023, 6:37pm UTC](https://discourse.julialang.org/t/q-vector-of-vectors-of-variable-lengths/102817/7 "2023-08-15T18:37:47Z")

</div>

Thanks to all who replied.

The broadcasting method suggested by abraemer and ohell is elegant.

```julia
# test_vector_of_vectors.jl

begin
    sizes = Int32[3, 6, 7]
    vec_of_vecs = zeros.(sizes)

    @show vec_of_vecs
end

```

```julia
3-element Vector{Vector{Float64}}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

```
