# Custom NamedTuple dynamic creation

**URL:** https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807
**Category:** General Usage
**Tags:** namedtuple
**Created:** [February 6, 2024, 1:59pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807 "2024-02-06T13:59:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ImreMD](https://avatars.discourse-cdn.com/v4/letter/i/43a26b/32.png) [@ImreMD](https://discourse.julialang.org/u/ImreMD)
#### Post date: [February 6, 2024, 1:59pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/1 "2024-02-06T13:59:34Z")

</div>

Hello guys, wondering if there’s a way of dynamically assign keys to a NamedTuple something like this:

```julia
 function create_named_tuple(x, y, z)
  mynames = (:position, :y, :z)
  mydata = (x, "free","get")
  return NamedTuple{mynames}(mydata)
 end

```

Written this way it doesn’t work the keys are literal string “y” and “z” instead of variables passed to the function. Is there a way of doing this without using additional libraries?

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 6, 2024, 2:57pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/2 "2024-02-06T14:57:53Z")

</div>

see if this helps you

```julia

julia> f(y)=(;Symbol(y)=>"ypsilon",)
f (generic function with 1 method)

julia> c='c'
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)

julia> f(c)
(c = "ypsilon",)

julia> c='z'
'z': ASCII/Unicode U+007A (category Ll: Letter, lowercase)

julia> f(c)
(z = "ypsilon",)

```

```julia

julia> NamedTuple{(Symbol(c),)}(("ypsilon",))
(z = "ypsilon",)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 6, 2024, 3:25pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/3 "2024-02-06T15:25:02Z")

</div>

You could write:

```julia
function create_named_tuple(x, y, z)
    mynames = (:position, Symbol(y), Symbol(z))
    mydata = (x, "free", "get")
    return NamedTuple{mynames}(mydata)
end

create_named_tuple("paid", "cost", "task")

```

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [February 6, 2024, 4:06pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/4 "2024-02-06T16:06:03Z")

</div>

Note that using dynamic keys like this may limit type stability and reduce performance in some situations. `NamedTuple` is designed with the idea that keys are usually known at compile time. You should consider whether `Dict` (which does not “compile in” they keys) is suitable for your use case.

---

<div class="post-metadata">

### Author: ![ImreMD](https://avatars.discourse-cdn.com/v4/letter/i/43a26b/32.png) [@ImreMD](https://discourse.julialang.org/u/ImreMD)
#### Post date: [February 6, 2024, 4:35pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/5 "2024-02-06T16:35:52Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> function create_named_tuple(x, y, z)
> mynames = (:position, Symbol(y), Symbol(z))
> mydata = (x, "free", "get")
> return NamedTuple{mynames}(mydata)
> end
> 
> create_named_tuple("paid", "cost", "task")
> 
> ```

Magic! Thanks. However I will take note of mikemoore remark (below) but my use case shouldn’t be so exigent on performance.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [February 6, 2024, 4:40pm UTC](https://discourse.julialang.org/t/custom-namedtuple-dynamic-creation/109807/6 "2024-02-06T16:40:32Z")

</div>

Hang on, are the `y` and `z` arguments not assigned to symbols to begin with? NamedTuples can only take symbols so why not constrain `y::Symbol` and `z::Symbol`? `Symbol(y)` is flexible but not everything should be forced into a symbol. A `Dict` wouldn’t need to do that to its keys.
