# Change dictionary values in array

**URL:** <https://discourse.julialang.org/t/change-dictionary-values-in-array/75520>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 31, 2022, 4:59pm UTC](https://discourse.julialang.org/t/change-dictionary-values-in-array/75520 "2022-01-31T16:59:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [January 31, 2022, 4:59pm UTC](https://discourse.julialang.org/t/change-dictionary-values-in-array/75520/1 "2022-01-31T16:59:52Z")

</div>

Person = fill(Dict(“x” =\> 0,“status” =\> 0,“color” =\> “black”),5)  
Person[1][“color”] = “green”  
HI  
I have an array of dictionaries. when I change the value of single dictionary, all other values in other dictionaries are affected.  
How to prevent this in Julia?

---

<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:** [January 31, 2022, 5:01pm UTC](https://discourse.julialang.org/t/change-dictionary-values-in-array/75520/2 "2022-01-31T17:01:59Z")

</div>

`fill` will place the same object into the resulting array, so that you change all instances when changing one. This behaviour is discussed in the docstring:

```julia
help?> fill
  fill(x, dims::Tuple)
  fill(x, dims...)

  Create an array filled with the value x. For example, fill(1.0, (5,5)) returns a 5×5 array of floats, with each element initialized to 1.0.

(...)

  If x is an object reference, all elements will refer to the same object:

  julia> A = fill(zeros(2), 2);
  
  julia> A[1][1] = 42; # modifies both A[1][1] and A[2][1]
  
  julia> A
  2-element Vector{Vector{Float64}}:
   [42.0, 0.0]
   [42.0, 0.0]

```

You can use a comprehension instead:

```julia
julia> person = [Dict("x" => 0, "status" => 0, "color" => "black") for _ ∈ 1:5]
5-element Vector{Dict{String, Any}}:
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")

julia> person[1]["color"] = "green"
"green"

julia> person
5-element Vector{Dict{String, Any}}:
 Dict("status" => 0, "x" => 0, "color" => "green")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")
 Dict("status" => 0, "x" => 0, "color" => "black")

```

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [January 31, 2022, 5:18pm UTC](https://discourse.julialang.org/t/change-dictionary-values-in-array/75520/3 "2022-01-31T17:18:51Z")

</div>

> [@nilshg](#):
>
> `_ ∈ 1:5]`

Worked perfectly, Many thanks
