How to insert a new vector in a vector of vectors

Hello,

I’m beginning with Julia.
Here is my problem: i have a vector of vectors that i fill up inside a loop under some conditions.
At starting, i have v=[[1]].
Then with push! and pushfirst!, i fill up v.
For example, after several passages inside the loop, v=[[3],[1,2],[4]].
Now how is it possible to insert inside v the vector 5 between [3] and [1,2] to get v=[[3],[5],[1,2],[4]] ?

I hope i make myself clear.

Thanks in advance for your help.

Try this:

julia> v=[[3],[1,2],[4]]
3-element Vector{Vector{Int64}}:
 [3]
 [1, 2]
 [4]

julia> insert!(v, 2, [5])
4-element Vector{Vector{Int64}}:
 [3]
 [5]
 [1, 2]
 [4]
1 Like

Perfect !
Thanks a lot

But be aware that in a general setting this might be suboptimal, moving the elements behind the inserted position in memory…