# Is using values() the correct way to obtain the namedtuple in keyword arguments?

**URL:** <https://discourse.julialang.org/t/is-using-values-the-correct-way-to-obtain-the-namedtuple-in-keyword-arguments/55074>\
**Category:** General Usage\
**Created:** [February 11, 2021, 6:06pm UTC](https://discourse.julialang.org/t/is-using-values-the-correct-way-to-obtain-the-namedtuple-in-keyword-arguments/55074 "2021-02-11T18:06:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [February 11, 2021, 6:06pm UTC](https://discourse.julialang.org/t/is-using-values-the-correct-way-to-obtain-the-namedtuple-in-keyword-arguments/55074/1 "2021-02-11T18:06:03Z")

</div>

Suppose I had the following method:

```julia
function character_information(name::String, age::Int32; c...)
     println("Name: $name Age: $age")
     c_values = values(c)
     println(c_values.Alignment)  
end

# We're not following D&D rules, so in my little game an intelligence
# level of 5 is considered high intelligence.
character_information("Queen Svetlana",25,Alignment="Chaotic Evil",Intelligence=5)

```

If I were to use `typeof(c)` it will return `Base.Iterators.Pairs{...}`. In the [documentation](https://docs.julialang.org/en/v1/base/collections/#Base.Iterators.Pairs) it says:

> Transforms an indexable container into an Dictionary-view of the same data.

If I were to use `values(c)` I would get the namedtuple that contains all the keyword-values that I passed into the function. Using a keyword(`c_values.Alignment`) I can access the associated value. Is that the correct way of accessing an individual value?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 11, 2021, 6:56pm UTC](https://discourse.julialang.org/t/is-using-values-the-correct-way-to-obtain-the-namedtuple-in-keyword-arguments/55074/2 "2021-02-11T18:56:04Z")

</div>

Values is defined to return `kw.data` where `kw` is a `Pair`, so in this case it does return what you want. It doesn’t need to return a `NamedTuple` though, that’s perhaps an implementation detail. If you specifically want a `NamedTuple` you might construct it by splatting as `(; c...)`, eg.

```julia
julia> f(; kw...) = kw;

julia> kw = f(x = 1, y = 2)
pairs(::NamedTuple) with 2 entries:
  :x => 1
  :y => 2

julia> (; kw...)
(x = 1, y = 2)

```

Once you have a `NamedTuple` you may access the contents through `c_values.Alignment`.

However if you simply want the value assigned to the keyword argument `Alignment`, you might skip all this and simply use `c[:Alignment]`. Eg:

```julia
julia> kw = f(x = 1, y = 2)
pairs(::NamedTuple) with 2 entries:
  :x => 1
  :y => 2

julia> kw[:y]
2

```
