# NamedTuple, how to assign values?

**URL:** https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933
**Category:** New to Julia
**Tags:** namedtuple
**Created:** [April 3, 2022, 7:47am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933 "2022-04-03T07:47:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [April 3, 2022, 7:47am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933/1 "2022-04-03T07:47:24Z")

</div>

Hello!

Suppose I have a line as such:

```julia
varNames = NamedTuple{(:key,:offset,:type,:ncol), Tuple{String,Int,DataType,Int}}

```

The typeof this is:

```julia
typeof(varNames)
DataType

```

Then my question is; how do I insert values into, i.e. construct a NamedTuple based on this data type?

Kind regards

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 3, 2022, 8:25am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933/2 "2022-04-03T08:25:26Z")

</div>

I don’t know why you want to do this, you’re probably looking for a `struct`.

That said,

```julia
julia> varNames(("hi",3,String,4))
(key = "hi", offset = 3, type = String, ncol = 4)

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [April 3, 2022, 9:05am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933/3 "2022-04-03T09:05:33Z")

</div>

Thanks!

I ended up doing it differently this time as you hinted at (dict, instead of struct), but knowledge is power, so thanks for sharing the correct way to do it! 😊

Kind regards

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 3, 2022, 9:14am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933/4 "2022-04-03T09:14:37Z")

</div>

That’s probably fine. Just wanted to point out that there are many advantages to a struct over a Dict for something like this, performance wise.

```julia
struct VarNames
    key::String
    offset::Int
    type::DataType
    ncol::Int
end

```

It gives you dispatch, clearer contract and more efficient storage than a `Dict{Symbol, Any}`. If you’re willing to void the dispatch you could have also just used a `NamedTuple` like you first suggested, but without the intermediate type alias:

```julia
x = (key="hi", offset=3, type=String, ncol=4)

```

But if it works it works.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 3, 2022, 11:05am UTC](https://discourse.julialang.org/t/namedtuple-how-to-assign-values/78933/5 "2022-04-03T11:05:10Z")

</div>

You can’t insert values in a NamedTuple (or a Tuple for what does it matter), as these are _immutable_ types, after they have been created you can’t modify them (you can still modify -but not reassign - mutable elements of it)
