# Using push!

**URL:** <https://discourse.julialang.org/t/using-push/30935>\
**Category:** New to Julia\
**Created:** [November 11, 2019, 4:56am UTC](https://discourse.julialang.org/t/using-push/30935 "2019-11-11T04:56:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [November 11, 2019, 4:56am UTC](https://discourse.julialang.org/t/using-push/30935/1 "2019-11-11T04:56:52Z")

</div>

I am using `push!` to store the value of an array in every iteration `t`. But, it does not work like I expect it to:

```julia
function trial(n)
  m=Any[]
  y=rand(10)
  for t in 1:n
    push!(m,y)
    for x in 10
      y[x]=rand()
    end
  end
  return m
end

```

On executing the above function for `n=2`, I get:

```julia
2-element Array{Any,1}:
 [0.3413498610074548, 0.9008040663934613, 0.10721629384140297, 0.5546157202036206, 0.794439983649
3246, 0.729274144801793, 0.1932248146434996, 0.2950370824859381, 0.9389225778706747, 0.0149528983
28812081]
 [0.3413498610074548, 0.9008040663934613, 0.10721629384140297, 0.5546157202036206, 0.794439983649
3246, 0.729274144801793, 0.1932248146434996, 0.2950370824859381, 0.9389225778706747, 0.0149528983
28812081]

```

As can be seen, the last known value of `y` is stored in all of `m` instead of the different values of `y` in each iteration. Is there something wrong in my understanding?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 11, 2019, 5:25am UTC](https://discourse.julialang.org/t/using-push/30935/2 "2019-11-11T05:25:41Z")

</div>

When you `push!(m,y)` you are actually storing into `m` a reference to the array `y`, instead of copying the contents. That’s why if you modify an element in `y`, you will see the change in all of the elements of `m`: because you are actually looking inside the same `y`.

Take a look at:

```julia
julia> m = Any[]
0-element Array{Any,1}

julia> y = rand(3)
3-element Array{Float64,1}:
 0.3799842355542209
 0.789767043536056
 0.8862550043340174

julia> push!(m,y)
1-element Array{Any,1}:
 [0.3799842355542209, 0.789767043536056, 0.8862550043340174]

julia> push!(m,y)
2-element Array{Any,1}:
 [0.3799842355542209, 0.789767043536056, 0.8862550043340174]
 [0.3799842355542209, 0.789767043536056, 0.8862550043340174]

julia> y[2] = 1000;

julia> m
2-element Array{Any,1}:
 [0.3799842355542209, 1000.0, 0.8862550043340174]
 [0.3799842355542209, 1000.0, 0.8862550043340174]

```

You could certainly modify your function to make a copy of the current `y` and put it into m.

```julia
function trial_copy(n)
  m=Any[]
  y=rand(10)
  for t in 1:n
    push!(m,copy(y))
    for x in 10
      y[x]=rand()
    end
  end
  return m
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 11, 2019, 5:29am UTC](https://discourse.julialang.org/t/using-push/30935/3 "2019-11-11T05:29:46Z")

</div>

I don’t understand, the effect of `rand(10)` and `[rand() for _ in 1:10]` are the same, why would you do this?

---

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [November 11, 2019, 5:30am UTC](https://discourse.julialang.org/t/using-push/30935/4 "2019-11-11T05:30:50Z")

</div>

This is just an example I posted to understand how push! works. I am trying to use it in another code of mine

---

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [November 11, 2019, 5:33am UTC](https://discourse.julialang.org/t/using-push/30935/5 "2019-11-11T05:33:35Z")

</div>

> [@jling](#):
>
> [rand() for \_ in 1:10]

Thank you. I was thinking if there was a less expensive way of doing it since using `copy` results in more allocations and hence more time.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 11, 2019, 5:34am UTC](https://discourse.julialang.org/t/using-push/30935/6 "2019-11-11T05:34:17Z")

</div>

ah, so this is not about how `push!` works, this is just shallow copy (which is the norm, one could say).

```julia
julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = a

julia> a[1]=3
3

julia> b
3-element Array{Int64,1}:
 3
 2
 3

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 11, 2019, 5:36am UTC](https://discourse.julialang.org/t/using-push/30935/7 "2019-11-11T05:36:41Z")

</div>

In languages where assignment creates an independent array, you can avoid the copy when you assign but the copy is still made later if you modify either of the two arrays. This is “copy on write” behavior, which is how both R and Matlab do this. So your example would allocate in any language.

---

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [November 18, 2019, 11:47am UTC](https://discourse.julialang.org/t/using-push/30935/8 "2019-11-18T11:47:56Z")

</div>

Also, can I store arrays column-wise instead of row-wise which can be seen is the default of `push!`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 18, 2019, 3:48pm UTC](https://discourse.julialang.org/t/using-push/30935/9 "2019-11-18T15:48:04Z")

</div>

They are not being stored row-wise, it’s just that when you have a vector of vectors, those inner vectors are printed in rows to fit more compactly on your screen.

```julia
julia> x = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> y = [x, x]
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [1, 2, 3]

julia> y[1]
3-element Array{Int64,1}:
 1
 2
 3

```
