# Convert Dict to NamedTuple

**URL:** <https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140>\
**Category:** New to Julia\
**Tags:** question, dictionary, namedtuple\
**Created:** [April 30, 2023, 7:27pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140 "2023-04-30T19:27:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [April 30, 2023, 7:27pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140/1 "2023-04-30T19:27:48Z")

</div>

I’m looking for a clean way to convert Dictionaries into NamedTuples.  
I found a helpful [topic](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/9), however, that solution only works for Dicts with symbol keys.

> julia\> (; Dict(:a =\> 5, :b =\> 6, :c =\> 7)…)  
> (a = 5, b = 6, c = 7)

When string keys are converted to symbols, the order reverses.  
I found a way to solve this, but I expect there is a more clean and efficient way to do this.  
Any suggestion?

My code:

> julia\> d = Dict(“a” =\> 5, “b” =\> 6, “c” =\> 7)  
> Dict{String, Int64} with 3 entries:  
> “c” =\> 7  
> “b” =\> 6  
> “a” =\> 5

> julia\> (; zip(Symbol.(keys(d))[end:-1:begin], [values(d)…][end:-1:begin])…)  
> (a = 5, b = 6, c = 7)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 30, 2023, 7:35pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140/2 "2023-04-30T19:35:23Z")

</div>

Dicts do not have order. Any order you see is coincidental.

```julia
julia> NamedTuple(Dict(:a=>1, :b=>2))
(a = 1, b = 2)

```

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [April 30, 2023, 7:42pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140/3 "2023-04-30T19:42:43Z")

</div>

Thanks for pointing that out.  
Than I suspect my solution can be reduced to:

> (; zip(Symbol.(keys(d)), values(d))…)

Is this the best way to covert a Dict with string keys to a NamedTuple or is the a cleaner and more efficient way?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 30, 2023, 7:49pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140/4 "2023-04-30T19:49:57Z")

</div>

> [@Brinkhuis](#):
>
> d = Dict(“a” =\> 5, “b” =\> 6, “c” =\> 7)

This one is at least clearer, in my opinion.

```julia
julia> d = Dict("a" => 5, "b" => 6, "c" => 7)
Dict{String, Int64} with 3 entries:
  "c" => 7
  "b" => 6
  "a" => 5

julia> NamedTuple((Symbol(key),value) for (key,value) in d)
(c = 7, b = 6, a = 5)

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 30, 2023, 8:13pm UTC](https://discourse.julialang.org/t/convert-dict-to-namedtuple/98140/5 "2023-04-30T20:13:30Z")

</div>

As a side note, if you do want the keys to be sorted (in insertion order), you can used an `OrderedDict` or `LittleDict` from the package [OrderedCollections](https://github.com/JuliaCollections/OrderedCollections.jl)
