# 'export' dictionary keys as variables

**URL:** <https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968>\
**Category:** General Usage\
**Created:** [January 23, 2019, 9:51am UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968 "2019-01-23T09:51:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 23, 2019, 9:51am UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/1 "2019-01-23T09:51:24Z")

</div>

Given a dictionary, how to create variables that point to the keys’ value.  
This is easy to do manually:

```julia
dict = Dict(:a => "newton", :b => "einstein", :c => "hawking")

a = dict[:a]
a # => "newton"

b = dict[:b]
b # => "einstein"

```

but how to do it automatically? My mind has gone blank… 😶

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 23, 2019, 10:29am UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/2 "2019-01-23T10:29:50Z")

</div>

Maybe like this?

```julia
julia> dict = Dict(:a => "newton", :b => "einstein", :c => "hawking");

julia> for key in keys(dict)
           @eval const $key = dict[$(QuoteNode(key))]
       end

julia> a, b, c
("newton", "einstein", "hawking")

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 23, 2019, 10:36am UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/3 "2019-01-23T10:36:24Z")

</div>

> [@fredrikekre](#):
>
> `for key in keys(dict) @eval const $key = dict[$(QuoteNode(key))] end`

Thanks, that’s brilliant…

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 23, 2019, 1:10pm UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/4 "2019-01-23T13:10:29Z")

</div>

I had

```julia
for key in keys(dict)
           @eval const $key = dict[$key]
       end

```

which didn’t work - what does the `QuoteNode` do?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 23, 2019, 1:42pm UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/5 "2019-01-23T13:42:24Z")

</div>

You need the quote node to ensure that the key is represented as a quoted symbol in the resulting expression. That is, you need it in order to get an expression like `dict[:x]` instead of `dict[x]`

---

<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 23, 2019, 2:24pm UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/6 "2019-01-23T14:24:52Z")

</div>

In addition, there is some documentation about it:

[https://docs.julialang.org/en/v1/manual/metaprogramming/#QuoteNode-1](https://docs.julialang.org/en/v1/manual/metaprogramming/#QuoteNode-1)  
[https://docs.julialang.org/en/v1/devdocs/ast/#Quote-expressions-1](https://docs.julialang.org/en/v1/devdocs/ast/#Quote-expressions-1)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 23, 2019, 5:16pm UTC](https://discourse.julialang.org/t/export-dictionary-keys-as-variables/19968/7 "2019-01-23T17:16:55Z")

</div>

On reflection I don’t really know what a ‘node’ is, which is why `QuoteNode`s weren’t easy to understand.
