# How does push!() work within accumulate()?

**URL:** <https://discourse.julialang.org/t/how-does-push-work-within-accumulate/81286>\
**Category:** General Usage\
**Tags:** accumulate, push\
**Created:** [May 18, 2022, 10:01pm UTC](https://discourse.julialang.org/t/how-does-push-work-within-accumulate/81286 "2022-05-18T22:01:40Z")\
**Posts on this page:** 2\
**Page:** 1

<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 18, 2022, 10:01pm UTC](https://discourse.julialang.org/t/how-does-push-work-within-accumulate/81286/1 "2022-05-18T22:01:40Z")

</div>

why are the two formulations of accumulate() different?

```julia

accumulate((s,c)->push!(s,c),1:3,init=[]) != accumulate((s,c)->[s;c],1:3,init=[]) #true

push!([1,2],3)==[[1,2];3] # true

```

---

<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:** [May 18, 2022, 11:32pm UTC](https://discourse.julialang.org/t/how-does-push-work-within-accumulate/81286/2 "2022-05-18T23:32:58Z")

</div>

`push!` mutates a vector, while concatenation with `[s; c]` creates a completely new vector. In the first case, you’re mutating that same initial vector `[]` over and over, so each element in the result is the _same_ vector and has the same value. In the second case, you’re creating a brand new vector with `[s; c]` at each iteration, so the accumulated results aren’t all identical.
