# 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:** 2

<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, 12:00pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/24 "2019-08-13T12:00:32Z")

</div>

I don’t think it is a good idea, because, as I remarked above, `Dicts` are not ordered, so it can happen that two `<:AbstractDict` values are `==` but the resulting `NamedTuple`s aren’t.

---

<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, 1:30pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/25 "2019-08-13T13:30:28Z")

</div>

It may be the version – I do not have v1.0.3 available …  
you have a way that works, so I am not looking further

---

<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:** [August 15, 2019, 10:33am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/26 "2019-08-15T10:33:59Z")

</div>

> [@pjgorski](#):
>
> And how to _make a named tuple from a dictionary_ , when dictionary keys are of type `String` ?

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

julia> (; (Symbol(k) => v for (k,v) in adict)...)
(bstr = 'b', astr = 5)

```

---

<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:** [August 15, 2019, 10:35am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/27 "2019-08-15T10:35:47Z")

</div>

> [@carstenbauer](#):
>
> Perhaps there should be a NamedTuple constructor taking a Dict and vice versa as this seems to pop up regularly. What do you think?

There already is a much more general constructor which takes any iterator yielding `Symbol => value` pairs. People just don’t know about the syntax (hence [add an example of programatically creating a named tuple by KristofferC · Pull Request #32330 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/32330)).

---

<div class="post-metadata">

**Author:** ![ElectronicTeaCup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/electronicteacup/32/12647_2.png) [@ElectronicTeaCup](https://discourse.julialang.org/u/ElectronicTeaCup)\
**Post date:** [February 8, 2020, 11:22am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/28 "2020-02-08T11:22:12Z")

</div>

> [@mauro3](#):
>
> (; Dict(:a =\> 5, :b =\> 6, :c =\> 7)…)

NamedTupleTools looks awesome! Is it possible to recursively turn all the dictionaries in the dictionary into named tuples?

Also what is your opinion on doing this in the first place? I am reading some MATLAB .mat files (which is read by MAT.jl to a dictionary) which have a nested matlab structs e.g EEG.event.epoch. And in order to keep my code more readable by the MATLAB savvy ones, I was thinking of using namedtuples since they resemble MATLAB structs. Are there any pros/cons do doing this? Order isn’t really neccessary.

---

<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:** [February 8, 2020, 12:44pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/29 "2020-02-08T12:44:19Z")

</div>

If you plan to go deep you may want to check whether [Performance of nested named tuples · Issue #25296 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/25296) is still a problem.

---

<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:** [February 9, 2020, 4:15am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/31 "2020-02-09T04:15:59Z")

</div>

That problem has been resolved (not an issue with v1.3.1).

---

<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:** [February 9, 2020, 4:24am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/32 "2020-02-09T04:24:50Z")

</div>

`NamedTuples` are a good choice anywhere they are a natural fit.  
It sounds like `NamedTuples` would be appropriate for your use.

---

<div class="post-metadata">

**Author:** ![ElectronicTeaCup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/electronicteacup/32/12647_2.png) [@ElectronicTeaCup](https://discourse.julialang.org/u/ElectronicTeaCup)\
**Post date:** [February 9, 2020, 10:45am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/33 "2020-02-09T10:45:24Z")

</div>

Okay great. So in this use case when I convert deeply nested MATLAB structs (eg. EEG.event.epoch) using MAT.jl the output will be an Dict named EEG containing a Dict called event. So if you need to access epoch it would be accessed by:

```julia
EEG["event"]["epoch"]

```

And so (finally) using NameTupleTools when I make a named tuple from EEG:

```julia
namedtuple(EEG)

```

EEG.event will contain event as a dict; requiring me to access epoch in the following way:

```julia
EEG.event["epoch"]

```

Is there way for the Dict constructor to convert all nested dictionaries into named tuples too? So that I can simply use:

```julia
EEG.event.epoch

```

---

<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:** [February 9, 2020, 11:10am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/34 "2020-02-09T11:10:31Z")

</div>

```julia
using NamedTupleTools
recursive_namedtuple(x::Any) = x
recursive_namedtuple(d::Dict) = namedtuple(Dict(k => recursive_namedtuple(v) for (k, v) in d))

```

You may want to restrict this to only apply to `Dict`s with the right type of keys.

---

<div class="post-metadata">

**Author:** ![ElectronicTeaCup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/electronicteacup/32/12647_2.png) [@ElectronicTeaCup](https://discourse.julialang.org/u/ElectronicTeaCup)\
**Post date:** [February 9, 2020, 1:38pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/35 "2020-02-09T13:38:29Z")

</div>

Awesome, thanks a lot! I didn’t even know keys and values could be accessed like that.

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 24, 2020, 1:13pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/36 "2020-09-24T13:13:40Z")

</div>

The shortest universal solution seems to be this:

```
merge(NamedTuple(), Dict(:a => 5, :b => 6, :c => 7))

```

---

<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:** [September 24, 2020, 1:23pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/37 "2020-09-24T13:23:01Z")

</div>

What…? @mauro3’s solution, which is also marked as the solution, seems shorter:

> [@mauro3](#):
>
> 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:** ![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:** [September 24, 2020, 1:23pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/38 "2020-09-24T13:23:11Z")

</div>

Perhaps you missed [this one](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/10)?

---

<div class="post-metadata">

**Author:** ![DiogoSantiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diogosantiago/32/30915_2.png) [@DiogoSantiago](https://discourse.julialang.org/u/DiogoSantiago)\
**Post date:** [January 19, 2022, 1:35am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/39 "2022-01-19T01:35:11Z")

</div>

Many of the answers here do not work if Dict is created using string keys, just for Symbol keys, probably we should have some easier functions for conversions, i got myself sometimes struggling with type conversions too…

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 19, 2022, 1:50pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/40 "2022-01-19T13:50:45Z")

</div>

If the keys are symbols as in

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

```

the best solution in Julia 1.6 is probably

```julia
NamedTuple(d)

```

Now if the keys are strings you must convert them to symbols. Here are some solutions:

```julia
d = Dict("a" => 5, "b" => 6, "c" => 7)

NamedTuple(Symbol(k) => v for (k,v) in d)

# or
NamedTuple(Symbol.(keys(d)) .=> values(d))

# or
(; (Symbol(k) => v for (k,v) in d)...)

# or
(; (Symbol.(keys(d)) .=> values(d))...)
```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [January 20, 2022, 11:48am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/41 "2022-01-20T11:48:29Z")

</div>

> [@sijo](#):
>
> `NamedTuple(Symbol(k) => v for (k,v) in d)`

If you have this a lot of times you can, of course, define

```julia
import Base.NamedTuple
NamedTuple(d::Dict{String, T} where T) = NamedTuple(Symbol(k) => v for (k,v) in d)

```

which allows you to do

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

```

---

<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:** [January 20, 2022, 11:51am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/42 "2022-01-20T11:51:25Z")

</div>

Strictly speaking this is type piracy, although I guess it’s unlikely to break things in practice.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [January 20, 2022, 12:14pm UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/43 "2022-01-20T12:14:09Z")

</div>

I agree. So the clean way would be

```julia
nt(d::Dict{AbstractString, T} where T) = NamedTuple(Symbol(k) => v for (k,v) in d)

```

with

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

```

But I think many people would like to see the above implementation as standard, perhaps with `String` replaced by `AbstractString` as here.

---

<div class="post-metadata">

**Author:** ![ultrapoci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ultrapoci/32/25356_2.png) [@ultrapoci](https://discourse.julialang.org/u/ultrapoci)\
**Post date:** [June 23, 2023, 9:34am UTC](https://discourse.julialang.org/t/how-to-make-a-named-tuple-from-a-dictionary/10899/45 "2023-06-23T09:34:15Z")

</div>

I might as well post my solution, which is also recursive and doesn’t use any external library:

```julia
namedtuple(d::Dict) = (; map(k -> Symbol(first(k)) => namedtuple(last(k)), collect(d))...)
namedtuple(x) = x

```

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

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