# The mutation function changes all the values in a dictionary

**URL:** <https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 6, 2023, 1:13pm UTC](https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602 "2023-01-06T13:13:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fordonez](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@fordonez](https://discourse.julialang.org/u/fordonez)\
**Post date:** [January 6, 2023, 1:13pm UTC](https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602/1 "2023-01-06T13:13:00Z")

</div>

Hi everyone.  
I am using a mutate function that changes the value of an array and then I save the array to a dictionary.  
The problem is that the function changes all the values saved in the dictionary.

```julia
a = Dict()
b = ones(2,2)

function update_b!(b)
    b .= b .+1
end

for i in 1:2
    update_b!(b)
    a[i] = b
end

```

The result of a:

```julia
  2 => [3.0 3.0; 3.0 3.0]
  1 => [3.0 3.0; 3.0 3.0]

```

I would like something like:

```julia
  2 => [3.0 3.0; 3.0 3.0]
  1 => [2.0 2.0; 2.0 2.0]

```

Using the function with a return fixes the problem, but I would like to know if the problem can be fixed with a mutable function.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 6, 2023, 1:20pm UTC](https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602/2 "2023-01-06T13:20:35Z")

</div>

> [@fordonez](#):
>
> `a[i] = b`

You are assigning the same object here to the left hand side. No copy is made implicitly. Changing `b` is thus reflected everywhere it is referenced.

Either assign `a[i] = copy(b)`, or construct the dictionary like

```julia
a = Dict(i=>ones(2,2).+i for i in 1:2)

```

---

<div class="post-metadata">

**Author:** ![fordonez](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@fordonez](https://discourse.julialang.org/u/fordonez)\
**Post date:** [January 6, 2023, 1:41pm UTC](https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602/3 "2023-01-06T13:41:22Z")

</div>

Thank you very much, it worked perfectly.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 6, 2023, 1:50pm UTC](https://discourse.julialang.org/t/the-mutation-function-changes-all-the-values-in-a-dictionary/92602/4 "2023-01-06T13:50:46Z")

</div>

Great! Then please mark my answer as the solution if you are happy with it and have no further questions, so others will know the topic needs no more immediate attention.
