# Create a dictionary from Dictionary and Array

**URL:** <https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382>\
**Category:** New to Julia\
**Created:** [November 12, 2021, 2:17pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382 "2021-11-12T14:17:31Z")\
**Posts on this page:** 7\
**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 12, 2021, 2:17pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/1 "2021-11-12T14:17:31Z")

</div>

Hello everyone,

I have an array a = [8,9] and a dictionary d = Dict(“c” = [1,3,4,6] , “b” = [2,5])  
How can I assign all the values of “c” and “b” to the corresponding values of “c” and “b” in a?  
Result : h = Dict(“c” = [8,8,8,8] , “b” = [9,9])

Thank you,

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 12, 2021, 2:20pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/2 "2021-11-12T14:20:24Z")

</div>

How do you know that `"c"` should correspond to the 8 and `"b"` to the 9?

---

<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 12, 2021, 2:38pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/3 "2021-11-12T14:38:44Z")

</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 a = [8,9] from column 2 of df2  
d = 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

---

<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:15am UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/4 "2021-11-15T10:15:12Z")

</div>

[quote=“jnewbie, post:3, topic:71382, full:true”]

> 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 a = [8,9] from column 2 of df2  
d = 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

---

<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:** [November 15, 2021, 10:58am UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/5 "2021-11-15T10:58:46Z")

</div>

I’m not sure how the DataFrames you posted twice now relate to the original question.

Your original question was how can I go from

```julia
julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

```

to a Dict which has values

```julia
julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

```

the answer to this question is

```julia
julia> d["c"] .= 8; d["b"] .= 9; 

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [9, 9]

```

Here I am overwriting the existing arrays stored in the dictionary with the numbers provided. If you don’t want to hardcode those numbers, you have to pick them out from the DataFrame, doing something like

```julia
julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

julia> for k ∈ keys(d)
           d[k] .= df2[df2.Column1 .== k, :Column2]
       end

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [9, 9]

```

---

<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, 12:21pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/7 "2021-11-15T12:21:01Z")

</div>

Thank you it works perfectly !

If it helps anyone, to create a new dictionary without modifying the previous one I did this:

```julia
d2 = Dict(k =>(copy(d[k]) .= df2[df2 .== k, :Column2]) for k ∈ keys(d))

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 15, 2021, 1:05pm UTC](https://discourse.julialang.org/t/create-a-dictionary-from-dictionary-and-array/71382/8 "2021-11-15T13:05:17Z")

</div>

`Accessors.jl` provides a simple way to set specific values in a dictionary without modifying the original dict:

```julia
using Accessors

julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])

julia> d2 = @set d["c"] = fill(8, 4)
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [2, 5]

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

```

You can also keep the `"c"` vector with the original length and assign its values:

```julia
julia> d2 = @set d["c"] |> Elements() = 8
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [2, 5]

```

This approach is convenient, but less efficient if you need lots of these assignments: the dict gets copied each time.
