# Unexpected behaviour when using ˋrepeatˋ

**URL:** https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875
**Category:** New to Julia
**Tags:** question
**Created:** [May 9, 2025, 1:41pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875 "2025-05-09T13:41:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![azeredo-e](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azeredo-e/32/208402_2.png) [@azeredo-e](https://discourse.julialang.org/u/azeredo-e)
#### Post date: [May 9, 2025, 1:41pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/1 "2025-05-09T13:41:01Z")

</div>

Hello everyone,

I’m encountering some difficulties when using the `repeat` function and repeating arrays. My objective is creating an array of arrays, applying some function to it and getting the array back with its new value appended to it. In my case I’m trying to parallelyse the operations, but here I’m just showing the minimal working example of my problem.

```julia
julia> x_list = repeat([[0.25]], 3)
3-element Vector{Vector{Float64}}:
 [0.25]
 [0.25]
 [0.25]

julia> r_list = [2.9, 3.0, 3.1]
3-element Vector{Float64}:
 2.9
 3.0
 3.1

julia> for (x, r) in zip(x_list, r_list)
    push!(x, x[1]+r)
end

```

Ideally my output would be

```julia
julia> x_list
3-element Vector{Vector{Float64}}:
 [0.25, 3.15]
 [0.25, 3.25]
 [0.25, 3.35]

```

But thats not the case, instead I get

```julia
julia> x_list
3-element Vector{Vector{Float64}}:
 [0.25, 3.15, 3.25, 3.35]
 [0.25, 3.15, 3.25, 3.35]
 [0.25, 3.15, 3.25, 3.35]

```

When I test `x_list[1] === x_list[2]` and `x_list[2] === x_list[3]`, I get `true` in both cases. So when ˋrepeatˋ creates the array, in my limited understanding, it sets every item as a pointer to the same array stored in memory so they are indistinguishable and my code doesn’t work as expected.

My question is: is this expected behaviour? I’ve tested `repeat` with integers and this doesn’t happen. Also, if it is expected, how can I solve my problem? In my specific implementation I still create an array with the initial condition, but the number of times I created is defined at runtime.

---

<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: [May 9, 2025, 1:54pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/2 "2025-05-09T13:54:12Z")

</div>

> [@azeredo-e](#):
>
> it sets every item as a pointer to the same array stored in memory

kinda, yeah, repeat creates a shallow copy:

```julia
julia> a=[[3]];

julia> b=copy(a);

julia> push!(b[1], 4);

julia> a
1-element Vector{Vector{Int64}}:
 [3, 4]

```

you can do something like this instead:

```julia
julia> as = [[0.25] for _ = 1:3];

julia> push!(as[1], 2);

julia> as
3-element Vector{Vector{Float64}}:
 [0.25, 2.0]
 [0.25]
 [0.25]

```

---

<div class="post-metadata">

### Author: ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)
#### Post date: [May 9, 2025, 1:55pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/3 "2025-05-09T13:55:34Z")

</div>

By creating `x_list = repeat([[0.25]], 3)` all elements share the same memory location:

```julia-repl
julia> x_list = repeat([[0.25]], 3)
3-element Vector{Vector{Float64}}:
 [0.25]
 [0.25]
 [0.25]

julia> x_list[1][1] = -1
-1

julia> x_list
3-element Vector{Vector{Float64}}:
 [-1.0]
 [-1.0]
 [-1.0]

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 9, 2025, 2:45pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/4 "2025-05-09T14:45:39Z")

</div>

This is a common gotcha; most commonly it crops up when you `fill` the same array into many locations. But `repeat`ing an array of arrays is exactly the same situation. It’s a general thing about the semantics of the language itself.

The docs for [`fill`](https://docs.julialang.org/en/v1/base/arrays/#Base.fill) explicitly call this out; you might find the explanation there helpful.

> Every location of the returned array is set to (and is thus [`===`](https://docs.julialang.org/en/v1/base/base/#Core.:===) to) the `value` that was passed; this means that if the `value` is itself modified, all elements of the `fill`ed array will reflect that modification because they’re _still_ that very `value`. This is of no concern with `fill(1.0, (5,5))` as the `value` `1.0` is immutable and cannot itself be modified, but can be unexpected with mutable values like — most commonly — arrays. For example, `fill([], 3)` places _the very same_ empty array in all three locations of the returned vector:
> 
> ```julia
> julia> v = fill([], 3)
> 3-element Vector{Vector{Any}}:
> []
> []
> []
> 
> julia> v[1] === v[2] === v[3]
> true
> 
> julia> value = v[1]
> Any[]
> 
> julia> push!(value, 867_5309)
> 1-element Vector{Any}:
> 8675309
> 
> julia> v
> 3-element Vector{Vector{Any}}:
> [8675309]
> [8675309]
> [8675309]
> 
> ```
> 
> To create an array of many independent inner arrays, use a [comprehension](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) instead. This creates a new and distinct array on each iteration of the loop:
> 
> ```julia
> julia> v2 = [[] for _ in 1:3]
> 3-element Vector{Vector{Any}}:
> []
> []
> []
> 
> julia> v2[1] === v2[2] === v2[3]
> false
> 
> julia> push!(v2[1], 8675309)
> 1-element Vector{Any}:
> 8675309
> 
> julia> v2
> 3-element Vector{Vector{Any}}:
> [8675309]
> []
> []
> 
> ```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 9, 2025, 3:57pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/5 "2025-05-09T15:57:01Z")

</div>

> [@mbauman](#):
>
> It’s a general thing about the semantics of the language itself.

Is it? What’s to stop `fill` or `repeat` from calling `copy`? Or if that’s not enough, a pair of `serialize`/`deserialize`?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 9, 2025, 4:23pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/6 "2025-05-09T16:23:21Z")

</div>

Yes, it’s true, they could do something different from what they currently are defined to do. And many folks have tried to make them do so.

The key language semantics that I’m thinking of here — that you’ll find _everywhere_ — are:

- An Array is an object that can be changed
- The same object can happily have multiple names and can even be placed in multiple locations in another array or datastructure
- Functions pass the object itself as the argument

Those are the fundamentals that are behind these behaviors. `fill(x, ...)` and `repeat([x], ...)` put that one thing that’s named `x` into multiple slots.

---

<div class="post-metadata">

### Author: ![azeredo-e](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azeredo-e/32/208402_2.png) [@azeredo-e](https://discourse.julialang.org/u/azeredo-e)
#### Post date: [May 10, 2025, 12:32am UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/7 "2025-05-10T00:32:22Z")

</div>

I see! It works now, thank you!

---

<div class="post-metadata">

### Author: ![azeredo-e](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azeredo-e/32/208402_2.png) [@azeredo-e](https://discourse.julialang.org/u/azeredo-e)
#### Post date: [May 10, 2025, 12:33am UTC](https://discourse.julialang.org/t/unexpected-behaviour-when-using-repeat/128875/8 "2025-05-10T00:33:07Z")

</div>

That makes sense, thank you!
