# How to make a named tuple from a dictionary?

**URL:** <https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899>\
**Category:** General Usage\
**Tags:** dictionary, namedtuple\
**Created:** [May 15, 2018, 2:30am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899 "2018-05-15T02:30:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 15, 2018, 2:30am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/1 "2018-05-15T02:30:13Z")

</div>

Is there a simple way to make a named tuple from a `Dict{Symbol, T}`?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 15, 2018, 4:15am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/2 "2018-05-15T04:15:22Z")

</div>

There should be an easier way, but this works:

````julia
julia> NamedTuple{tuple(keys(di)...), Tuple{typeof.(collect(values(di)))...,}}((values(di)...,)) 
(a = 1, b = 5.6)```
````

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 15, 2018, 4:20am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/3 "2018-05-15T04:20:05Z")

</div>

```julia
dictkeys(d::Dict) = (collect(keys(d))...,)
dictvalues(d::Dict) = (collect(values(d))...,)

namedtuple(d::Dict{Symbol,T}) where {T} =
    NamedTuple{dictkeys(d)}(dictvalues(d))

dictionary = Dict(:a => 5, :b => 6, :c => 7)
Dict{Symbol,Int64} with 3 entries:
  :a => 5
  :b => 6
  :c => 7

namedtuple(dictionary)
(a = 5, b = 6, c = 7)

dictionary = Dict(:a => 5, :b => 6.0, :c => "7")
Dict{Symbol,Any} with 3 entries:
  :a => 5
  :b => 6.0
  :c => "7"

namedtuple(dictionary)
(a = 5, b = 6.0, c = "7")

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 15, 2018, 5:52am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/4 "2018-05-15T05:52:53Z")

</div>

Thanks @mauro3 and @JeffreySarnoff for the replies, it would be nice of you to make a PR!

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 25, 2019, 10:04am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/5 "2019-01-25T10:04:48Z")

</div>

Is there nowadays a more convenient way to turn a dict into a named tuple?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 25, 2019, 10:18am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/6 "2019-01-25T10:18:38Z")

</div>

```julia
julia> d = Dict(:a => 1, :b => 2)
Dict{Symbol,Int64} with 2 entries:
  :a => 1                        
  :b => 2

julia> NamedTuple{Tuple(keys(d))}(values(d))
(a = 1, b = 2)

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 25, 2019, 10:33am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/7 "2019-01-25T10:33:51Z")

</div>

Probably touching internal detail, but…

```julia
((; kw...) -> kw.data)(; d...)

```

also works.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [January 25, 2019, 10:37am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/8 "2019-01-25T10:37:34Z")

</div>

AFAIU `values(kw)` is the public accessor for `kw.data` (to turn the keyword iterator into a `NamedTuple`).

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [January 25, 2019, 11:02am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/9 "2019-01-25T11:02:55Z")

</div>

Simply unpack the dict into a tuple.

```julia

(a = 1, dict...)

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 25, 2019, 11:04am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/10 "2019-01-25T11:04:45Z")

</div>

Tada:

```julia
julia> (; Dict(:a => 5, :b => 6, :c => 7)...)
(a = 5, b = 6, c = 7)

```

(Edit: note, that this already worked in Julia 0.7 when NamedTuples first appeared. Sometimes it just takes a bit longer to find the best solution…)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 1, 2019, 3:11pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/11 "2019-03-01T15:11:02Z")

</div>

Ran into the **inverse** of this problem, so thought it might be helpful (at least not harmful) to post a solution for that here:

```julia
julia> nt = (a = 5, b = 6, c = 7)
(a = 5, b = 6, c = 7)

julia> Dict(pairs(nt))
Dict{Symbol,Int64} with 3 entries:
  :a => 5
  :b => 6
  :c => 7

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 1, 2019, 3:13pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/12 "2019-03-01T15:13:48Z")

</div>

I think it would be nice if @mauro3’s solution were to appear explicitly in the documentation somewhere, and it might also be nice if there were some sort of `namedtuple` function, analogous to `tuple` which would probably make this functionality much easier to find.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 2, 2019, 5:55am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/13 "2019-03-02T05:55:10Z")

</div>

[NamedTupleTools.jl](https://github.com/JeffreySarnoff/NamedTupleTools.jl) exports `namedtuple`. I’d welcome issues that request additional features and be happier still with targeted PRs.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 22, 2019, 2:50pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/14 "2019-05-22T14:50:34Z")

</div>

Ran into this again and just wanted to point out that the solution here seems incredibly slow

```julia
julia> @btime (;d...)
  967.857 ns (13 allocations: 928 bytes)
(a = 1, b = 2)

```

compare that to the inverse

```julia
julia> @btime Dict(pairs(nt))
  208.054 ns (7 allocations: 704 bytes)
Dict{Symbol,Int64} with 2 entries:
  :a => 1
  :b => 2

```

Anyone know of any way of speeding this up?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [May 22, 2019, 4:10pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/17 "2019-05-22T16:10:17Z")

</div>

@Tamas_Papp solution seems twice as fast than `(; d...)`. But given that this is type-unstable code, it’s maybe not so suprising that it’s slow?

---

<div class="post-metadata">

**Author:** ![pjgorski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjgorski/32/4802_2.png) [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Post date:** [August 13, 2019, 10:19am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/19 "2019-08-13T10:19:54Z")

</div>

And how to _make a named tuple from a dictionary_, when dictionary keys are of type `String`?  
The proposed methods do not work in such a case.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 13, 2019, 10:40am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/20 "2019-08-13T10:40:03Z")

</div>

if using `NamedTupleTools.jl` is ok:

```julia
julia> using NamedTupleTools

julia> adict = Dict("astr" => 5, "bstr" => 'b');

julia> namedtuple(Symbol.(keys(adict)), values(adict))
(bstr = 'b', astr = 5)

julia> typeof(ans)
NamedTuple{(:bstr, :astr),Tuple{Char,Int64}}

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 13, 2019, 10:56am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/21 "2019-08-13T10:56:43Z")

</div>

```julia
julia> d = Dict(["a" => 1, "b" => 2])
Dict{String,Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> NamedTuple{Tuple(Symbol.(keys(d)))}(values(d))
(b = 2, a = 1)

```

Note that of course since a `Dict` not sorted, ordering is not guaranteed.

---

<div class="post-metadata">

**Author:** ![pjgorski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjgorski/32/4802_2.png) [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Post date:** [August 13, 2019, 11:32am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/22 "2019-08-13T11:32:59Z")

</div>

Thank you @Tamas_Papp. It worked.

> [@JeffreySarnoff](#):
>
> namedtuple(Symbol.(keys(adict)), values(adict))

This produces me an error:

```julia
MethodError: no method matching namedtuple(::Array{Symbol,1}, ::Base.ValueIterator{Dict{String,Any}})

```

(I am using Julia 1.0.3)

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [August 13, 2019, 11:50am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/23 "2019-08-13T11:50:15Z")

</div>

Perhaps there should be a NamedTuple constructor taking a Dict and vice versa as this seems to pop up regularly. What do you think?

[Next page](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899.md?page=2)
