# Question regarding push! and array of arrays

**URL:** <https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695>\
**Category:** New to Julia\
**Tags:** question, array\
**Created:** [December 11, 2020, 11:32pm UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695 "2020-12-11T23:32:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 11, 2020, 11:32pm UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695/1 "2020-12-11T23:32:05Z")

</div>

Hello, I have a question about a very unexpected behavior I encountered, I’d like an ELI5.

Why does this:

```julia
vi = fill(Int[], 5)
vi[2] = [5,6]
push!(vi[3], 9)

```

result in

```julia
 [9]
 [5, 6]
 [9]
 [9]
 [9]

```

instead of in

```julia
 []
 [5, 6]
 [9]
 []
 []

```

?

While we’re at it, why was the `[5, 6]` part left unchanged, unlike all the other elements?

BTW, my intention was to use a dynamic data structure consisting of `n` (determined at run time) independent dynamically created arrays, what’s the right way to do this (in case I’m doing it wrong)?

(Julia 1.5.3)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 11, 2020, 11:36pm UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695/2 "2020-12-11T23:36:26Z")

</div>

```julia
julia> vi = [Int[] for _=1:5]
5-element Array{Array{Int64,1},1}:
 []
 []
 []
 []
 []

julia> vi[2] = [5,6]
2-element Array{Int64,1}:
 5
 6

julia> push!(vi[3], 9)
1-element Array{Int64,1}:
 9

julia> vi
5-element Array{Array{Int64,1},1}:
 []
 [5, 6]
 [9]
 []
 []

```

Long story short, `fill` with `Int` arrays will just add “references” to the same array, not separate copies of arrays.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 11, 2020, 11:38pm UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695/3 "2020-12-11T23:38:37Z")

</div>

The reason is order of operations. This is a common trip-up. In the expression:

```julia
fill(Int[], 5)

```

First the the array `Int[]` is created, then the “outer” (5-element) array is created, and each element of that array references the empty `Int[]` array. These are all references to the same array, so when you modify one of them (by pushing) you modify them all.

It breaks down like this:

```julia
x = Int[]
vi = [x for _ in 1:5] # vi == [[], [], [], [], []]
push!(x, 1) # vi == [[1], [1], [1], [1], [1]]
vi[2] = [5, 6] # vi == [[1], [5, 6], [1], [1], [1]]

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 11, 2020, 11:41pm UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695/4 "2020-12-11T23:41:26Z")

</div>

Thank you both! I learned something.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 12, 2020, 7:10am UTC](https://discourse.julialang.org/t/question-regarding-push-and-array-of-arrays/51695/5 "2020-12-12T07:10:25Z")

</div>

You might be looking for

```julia
julia> vi = [Int[] for _ ∈ 1:5];

julia> vi[2] = [5, 6]
2-element Vector{Int64}:
 5
 6

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

julia> vi
5-element Vector{Vector{Int64}}:
 []
 [5, 6]
 [9]
 []
 []

```

Here the comprehension creates distinct `[]` objects.
