# How to insert a new vector in a vector of vectors

**URL:** https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678
**Category:** General Usage
**Tags:** question, vector
**Created:** [December 6, 2021, 8:34pm UTC](https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678 "2021-12-06T20:34:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)
#### Post date: [December 6, 2021, 8:34pm UTC](https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678/1 "2021-12-06T20:34:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 6, 2021, 8:36pm UTC](https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678/2 "2021-12-06T20:36:59Z")

</div>

Try this:

```julia
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]

```

---

<div class="post-metadata">

### Author: ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)
#### Post date: [December 6, 2021, 8:38pm UTC](https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678/3 "2021-12-06T20:38:48Z")

</div>

Perfect !  
Thanks a lot

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [December 6, 2021, 8:39pm UTC](https://discourse.julialang.org/t/how-to-insert-a-new-vector-in-a-vector-of-vectors/72678/4 "2021-12-06T20:39:11Z")

</div>

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