# Create array of empty (separate) vectors

**URL:** https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493
**Category:** General Usage
**Created:** [May 8, 2023, 4:12pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493 "2023-05-08T16:12:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [May 8, 2023, 4:12pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/1 "2023-05-08T16:12:17Z")

</div>

I want to initialise a number of (in this case empty) vectors. However, if I do e.g.

```julia
x = fill([],4)
push!(x[1],3)
x[2]

```

I get `[3]`. Basically, I put in the same vector 4 times, so now when I modify one, I modify all.

What is the workaround for this?

---

<div class="post-metadata">

### Author: ![filtron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filtron/32/49692_2.png) [@filtron](https://discourse.julialang.org/u/filtron)
#### Post date: [May 8, 2023, 4:17pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/2 "2023-05-08T16:17:53Z")

</div>

`x = [[] for in in 1:4 ]` should work.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [May 8, 2023, 4:18pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/3 "2023-05-08T16:18:49Z")

</div>

thanks 🙂

---

<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: [May 8, 2023, 5:24pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/4 "2023-05-08T17:24:49Z")

</div>

You should probably add a type annotation to your vectors. E.g.

```julia
x = [Int[] for in in 1:4 ]

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 8, 2023, 9:26pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/5 "2023-05-08T21:26:59Z")

</div>

```julia
julia> vov=Vector{Vector{Int}}()
Vector{Int64}[]

julia> push!(vov,[],[],[])
3-element Vector{Vector{Int64}}:
 []
 []
 []

julia> vov
3-element Vector{Vector{Int64}}:
 []
 []
 []

julia> 

julia> push!(vov[2],22)
1-element Vector{Int64}:
 22

julia> vov
3-element Vector{Vector{Int64}}:
 []
 [22]
 []

```

```julia
 vov=Vector{Vector{Int}}()
foreach(_->push!(vov,[]),1:4)

```

---

<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: [May 8, 2023, 10:27pm UTC](https://discourse.julialang.org/t/create-array-of-empty-separate-vectors/98493/6 "2023-05-08T22:27:56Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `push!(vov,[],[],[])`

This creates three `Vector{Any}` that are subsequently converted to `Vector{Int}`. Is that efficient?
