# Keep the dictionary unchanged

**URL:** https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484
**Category:** New to Julia
**Tags:** dictionary
**Created:** [November 15, 2021, 9:26am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484 "2021-11-15T09:26:58Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)
#### Post date: [November 15, 2021, 9:26am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/1 "2021-11-15T09:26:58Z")

</div>

Hi everyone,

I have a dictionary D1 and I´d like to create a new dictionary D2 with the same keys as D1 and  
by replacing all the values in each key of D1.  
I did this :

```julia
for i in 1:length(CT)
   D2 = Dict(k => (D1[k] .= CT[i]) for k in keys(D1))
end

```

This works but the D1 dictionary also changes. How can I keep the values in D1 unchanged?

Thank you,

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 15, 2021, 9:53am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/2 "2021-11-15T09:53:54Z")

</div>

The key question is how the indices of CT are supposed to relate to the keys of `D1` but without more information I would suggest

```julia
D2 = Dict(k => CT[k] for k in keys(D1))

```

---

<div class="post-metadata">

### Author: ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)
#### Post date: [November 15, 2021, 10:03am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/3 "2021-11-15T10:03:20Z")

</div>

> df1 = DataFrame(Column1 = [“c”,“b”,“c”,“c”,“b”,“c”],  
> Column2 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”],  
> Column3 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”] )

> df2 = DataFrame(Column1 = [“c”,“b”],  
> Column2 = [8,9],  
> Column3 = [“Value1”,“Value2”,])

So the array CT = [8,9] from column 2 of df2  
D1 = Dict(“c” = [1,3,4,6] , “b” = [2,5]) has as keys c and b and as values the indexes of c and b in the dataframe df1  
D2 = Dict(“c” = [8,8,8,8] , “b” = [9,9]) has as keys c and b and as values what corresponds to c and b in CT.

---

<div class="post-metadata">

### Author: ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)
#### Post date: [November 15, 2021, 10:07am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/4 "2021-11-15T10:07:45Z")

</div>

k is a string so I can´t use it as an index.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 15, 2021, 10:11am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/5 "2021-11-15T10:11:48Z")

</div>

Sorry, I’ve never used `DataFrames` and I don’t understand the logic of what you’re trying to do, so I’ll leave this to someone with better insights.

---

<div class="post-metadata">

### Author: ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)
#### Post date: [November 15, 2021, 10:17am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/7 "2021-11-15T10:17:15Z")

</div>

Ok, thanks for trying to help me !

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [November 15, 2021, 10:20am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/8 "2021-11-15T10:20:16Z")

</div>

`Dict(k => fill(CT[i], size(D1[k])...) for k in keys(D1))` might work. Do note that Julia’s dictionaries aren’t ordered, so the double iteration you’re doing can probably yield unexpected values.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 15, 2021, 10:20am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/9 "2021-11-15T10:20:20Z")

</div>

If you mutate the vectors that you store in D1, then yes, D1 will be changed. You need to copy the vectors if you need separate instances for both dictionaries.

So maybe (without trying it out)

```julia
for i in 1:length(CT)
   D2 = Dict(k => (copy(D1[k]) .= CT[i]) for k in keys(D1))
end

```

---

<div class="post-metadata">

### Author: ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)
#### Post date: [November 15, 2021, 10:29am UTC](https://discourse.julialang.org/t/keep-the-dictionary-unchanged/71484/10 "2021-11-15T10:29:54Z")

</div>

Thank you it worked !
