# Array of Vectors with Different Lengths

**URL:** <https://discourse.julialang.org/t/array-of-vectors-with-different-lengths/74107>\
**Category:** New to Julia\
**Created:** [January 5, 2022, 9:55pm UTC](https://discourse.julialang.org/t/array-of-vectors-with-different-lengths/74107 "2022-01-05T21:55:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [January 5, 2022, 9:55pm UTC](https://discourse.julialang.org/t/array-of-vectors-with-different-lengths/74107/1 "2022-01-05T21:55:09Z")

</div>

I am trying to create a data structure that can hold vectors that have different lengths, and I don’t know how many vectors I will have to store. I have been struggling with coming up with a method that can accomplish this. I have developed the following code to accomplish this:

```julia
function TestFunction(PreVec)
    for i=1:5
        Number = rand(1:10)
        NewVec = rand(1:10,Number,1 )
        Combined = [PreVec, NewVec]
        PreVec = Combined
        println(size(Combined))
        end
end
NumberInit = rand(1:10)
PreVec = rand(1:10,NumberInit,1 )
TestFunction(PreVec)

```

The issue with the above method is that I want my code to generate an array that has a length equivalent to the number of vectors that have been stored in the array. What the code above produces however is a nested array so that it is always of size 2. How could I modify the code above or use a different method to avoid this issue that I have of the nested arrays?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [January 5, 2022, 10:09pm UTC](https://discourse.julialang.org/t/array-of-vectors-with-different-lengths/74107/2 "2022-01-05T22:09:38Z")

</div>

Use `push!` to add each new vector to a vector of vectors.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 5, 2022, 10:11pm UTC](https://discourse.julialang.org/t/array-of-vectors-with-different-lengths/74107/3 "2022-01-05T22:11:25Z")

</div>

You probably just want to `push!` your new vector onto the vector of vectors:

```julia
ragged_array = []
push!(ragged_array, rand(4))
push!(ragged_array, rand(2))
# etc

```

PS: this takes me way back, my first “public” Julia-code had some ragged array stuff in it: [maurow / LMesh.jl / commit / d67401b22e44 — Bitbucket](https://bitbucket.org/maurow/lmesh.jl/commits/d67401b22e44e1a88ae8e349ec4335e3dfcaabd1)
