# Construct NamedTuple dynamically

**URL:** https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394
**Category:** General Usage
**Tags:** namedtuple
**Created:** [September 23, 2018, 6:26pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394 "2018-09-23T18:26:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [September 23, 2018, 6:26pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/1 "2018-09-23T18:26:45Z")

</div>

In Julia 1.0, I can constructed a named tuple with known names and values.

```julia
x = (a = 1, b = 2.0, c = "hello world")

```

Let’s say I have the names and values in separate arrays. How could I construct a named tuple dynamically?

```julia
mynames = ["a", "b", "c"]
myvalues = [1, 2.0, "hello world"]

```

---

<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: [September 23, 2018, 6:32pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/2 "2018-09-23T18:32:02Z")

</div>

```julia
julia> mynames = (:a, :b, :c);

julia> myvalues = [1, 2.0, "hello world"];

julia> NamedTuple{mynames}(myvalues)
(a = 1, b = 2.0, c = "hello world")

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [September 23, 2018, 6:33pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/3 "2018-09-23T18:33:17Z")

</div>

Answering my own question. I should have read this post first.

> [@\[ANN\] NamedTupleTools.jl](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374):
>
> [NamedTupleTools](https://github.com/JeffreySarnoff/NamedTupleTools.jl) is a small package that makes doing a few things with NamedTuples a bit easier. (Julia v1.0) using NamedTupleTools julia\> namedtuple(:a, :b, :c)(1, 2.0, "three") (a = 1, b = 2.0, c = "three") #= namedtuple( name1, name2, .. ) namedtuple( (name1, name2, ..) ) where the `names` are all `Symbols` or all `Strings` Generate a NamedTuple prototype by specifying or obtaining the fieldnames. The prototype is applied to fieldvalues, giving a completed NamedTuple. =# …

The solution:

```julia
julia> using NamedTupleTools

julia> nt = namedtuple(Symbol.(["a", "b", "c"])...)
NamedTuple{(:a, :b, :c),T} where T<:Tuple

julia> nt.([1, 2.0, "hello world"]...)
(a = 1, b = 2.0, c = "hello world")

```

---

<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: [September 23, 2018, 6:34pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/4 "2018-09-23T18:34:54Z")

</div>

Hm, I don’t see any advantage in using an external package here.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [September 23, 2018, 6:35pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/5 "2018-09-23T18:35:44Z")

</div>

Please enlighten me… Is there an easier way?

---

<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: [September 23, 2018, 6:36pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/6 "2018-09-23T18:36:09Z")

</div>

What about the answer I posted?

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [September 23, 2018, 6:37pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/7 "2018-09-23T18:37:38Z")

</div>

Haha… I just need to scroll up 🙂

---

<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: [February 27, 2020, 2:00pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/8 "2020-02-27T14:00:10Z")

</div>

Sorry for necroing but I see people still liking my post ([Construct NamedTuple dynamically - #2 by kristoffer.carlsson](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/2)). It is likely better to use:

```julia
julia> mynames = (:a, :b, :c);

julia> myvalues = [1, 2.0, "hello world"];

julia> (;zip(mynames, myvalues)...)
(a = 1, b = 2.0, c = "hello world")

```

---

<div class="post-metadata">

### Author: ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)
#### Post date: [February 27, 2020, 2:28pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/9 "2020-02-27T14:28:22Z")

</div>

Can you elaborate on why it is better to use `(;zip(mynames, myvalues)...)` rather than `NamedTuple{mynames}(myvalues)`?  
The later appear more readible to me and some basic benchmarks showed “better” performance in terms of allocations and execution time for a small named tuple.

---

<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: [February 27, 2020, 2:57pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/10 "2020-02-27T14:57:55Z")

</div>

Hm, well, if you have them as two arrays, one with symbols and one with values then perhaps the first way is better. But the one with the splatting is more general and you can use it without having to “unzip” the arrays in case you have

```julia
julia> v = [(:a, 1), (:b, 2)]
2-element Array{Tuple{Symbol,Int64},1}:
 (:a, 1)
 (:b, 2)

julia> (; v...)
(a = 1, b = 2)

```

I don’t think benchmarking this is too useful, it is very unlikely that converting to NamedTuples will take up and significant fraction of runtime of your program.

---

<div class="post-metadata">

### Author: ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)
#### Post date: [February 27, 2020, 3:35pm UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/11 "2020-02-27T15:35:20Z")

</div>

Thanks for the explanation!  
I agree with you, I was just wondering if there is a “technical” reason to use `(; v...)` over the other version or if it is more about convenience or personal preferences.

---

<div class="post-metadata">

### Author: ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)
#### Post date: [February 8, 2022, 5:04am UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/12 "2022-02-08T05:04:30Z")

</div>

The current [documentation](https://docs.julialang.org/en/v1/base/base/#Core.NamedTuple) for NamedTuple says:

> In a similar fashion as to how one can define keyword arguments programmatically,  
> a named tuple can be created by giving a pair `name::Symbol => value` or splatting  
> an iterator yielding such pairs after a semicolon inside a tuple literal:

```jldoctest
julia> (; :a => 1)
(a = 1,)

julia> keys = (:a, :b, :c); values = (1, 2, 3);

julia> (; zip(keys, values)...)
(a = 1, b = 2, c = 3)

```

This implies that `zip(keys, values)...` evaluates to Pairs of `name::Symbol => value`. Instead it evaluates to an array of tuples as you show above. When evaluated inside `(; )` that array is used to construct a named tuple. Granted, the array of tuples consists of `(::Symbol, value)` elements. Nevertheless for someone absorbing the language, the wording was confusing. I prefer your example for documentation!

---

<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: [February 8, 2022, 9:11am UTC](https://discourse.julialang.org/t/construct-namedtuple-dynamically/15394/13 "2022-02-08T09:11:06Z")

</div>

The documentation is accurate: `zip` doesn’t allocate an array, it returns an iterator (without allocation). Of course an array is iterable so you can use the `(; A...)` syntax with an array, it’s just not what happens here with `zip`.

When I have an iterator of name-value pairs, my preference is to use the `NamedTuple(iterator)` constructor:

```julia
keys = (:a, :b, :c); values = (1, 2, 3)
it = zip(keys, values)

julia> NamedTuple(it)
(a = 1, b = 2, c = 3)

```

But the `(; ...)` syntax is more flexible, it’s super useful in more complex cases:

```julia
keys = (:a, :b, :c); values = (1, 2, 3)
it = zip(keys, values)

A = [:c => 300, :d => 400]

mykey = Symbol("e", rand(3:5))
myval = 5000

other = (e="X", f="Y", g="Z")

julia> (; it..., b=20, A..., mykey => myval, other.f)
(a = 1, b = 20, c = 300, d = 400, e4 = 5000, f = "Y")

```

This takes the key-values from `it`, then overrides the `b` value, then takes the key-values from `A` (overriding `c` and adding `d`), then adds an element with a dynamically generated name, and finally adds an element from `other`, reusing the name.
