# Convert value type of dictionary

**URL:** <https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419>\
**Category:** New to Julia\
**Created:** [August 31, 2021, 10:53am UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419 "2021-08-31T10:53:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [August 31, 2021, 10:53am UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419/1 "2021-08-31T10:53:20Z")

</div>

Hello!

How would I convert Dict{String,String} to Dict{String,Float}? I have got this far so that I can convert the values:

```
map(v->parse(Float64,v),values(mydict))

```

But how can I match these with the original keys and put them back into the dictionary? keys(mydict) are the keys but they are not necessarily in the same order as values?

---

<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:** [August 31, 2021, 11:03am UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419/2 "2021-08-31T11:03:01Z")

</div>

```julia
julia> f((k,v)) = k => parse(Float64, v)
f (generic function with 1 method)      
                                        
julia> Dict(Iterators.map(f, pairs(d)))
Dict{String, Float64} with 1 entry:    
  "a" => 1.0                           

# slower than the above for repeated use on the REPL, but if you like Generators
julia> Dict(k => parse(Float64, v) for (k,v) in pairs(d)) 
Dict{String, Float64} with 1 entry:                      
  "a" => 1.0                                             

```

> [@Mastomaki](#):
>
> they are not necessarily in the same order as values?

`keys(mydict)` returns a `KeySet`, which is unordered because the `Dict` in `Base` is an unordered dict. If you require an ordered dict, check out `DataStructures.jl`, but in the majority of use cases you won’t need an ordered one.

---

<div class="post-metadata">

**Author:** ![George\_Morrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george_morrison/32/208189_2.png) [@George\_Morrison](https://discourse.julialang.org/u/George_Morrison)\
**Post date:** [April 1, 2024, 12:53pm UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419/3 "2024-04-01T12:53:08Z")

</div>

I am new to Julia and I am not sure what version you need to use my suggested solution.

‘’’  
myphonebook = Dict(“Jenny” =\> “8675309”, “Ghostbusters” =\> “5552368”)  
myphonebook = Dict(keys(myphonebook) .=\> parse.(Float64, values(myphonebook)))

Results:  
Dict{String, Float64} with 2 entries:  
“Jenny” =\> 8.67531e6  
“Ghostbusters” =\> 5.55237e6  
‘’’  
As @rocco_sprmnt21 noted my answer was incomplete. I think I have corrected it.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 1, 2024, 1:56pm UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419/4 "2024-04-01T13:56:00Z")

</div>

```julia
Dict((=>).(first(zip(d...)),parse.(Float64,last(zip(d...)))))

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 1, 2024, 8:03pm UTC](https://discourse.julialang.org/t/convert-value-type-of-dictionary/67419/5 "2024-04-01T20:03:06Z")

</div>

A little cleaner (oops, I see the marked answer gives this too):

```julia
julia> myphonebook = Dict("Jenny" => "8675309",
  "Ghostbusters" => "5552368");

julia> Dict(k=>parse(Float64,v) for (k,v) in myphonebook)
Dict{String, Float64} with 2 entries:
  "Jenny" => 8.67531e6
  "Ghostbusters" => 5.55237e6

```
