# Named tuples - construction, unpacking

**URL:** <https://discourse.julialang.org/t/named-tuples-construction-unpacking/51074>\
**Category:** General Usage\
**Tags:** question, namedtuple\
**Created:** [December 2, 2020, 1:54am UTC](https://discourse.julialang.org/t/named-tuples-construction-unpacking/51074 "2020-12-02T01:54:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [December 2, 2020, 1:54am UTC](https://discourse.julialang.org/t/named-tuples-construction-unpacking/51074/1 "2020-12-02T01:54:57Z")

</div>

I stumbled by accident upon the fact that you can create a named tuple by naming one or more of the elements, not all of them. At least, it seems, as long as the un-named elements are given by existing variables:

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

julia> foo = (a, b) # regular tuple
(1, 2)

julia> bar = (a=a, b) # only named first element
(a = 1, b = 2)

julia> baz = (a, b=b) # only named second element
(a = 1, b = 2)

julia> qux = (a=3, b) # values don't have to be variables
(a = 3, b = 2)

```

I don’t see this behavior mentioned in the docs.

Also tangentially related: I was wondering if there’s a technique to tersely “unpack” a named tuple into a local scope without having to explicitly name the variables being unpacked into. I’m thinking of (ab)using named tuples as kind of anonymous, implicitly-defined struct instances that can be used to carry related values around, and easily use the contained values by name without having to use dot syntax (for clarity) nor explicitly unpack into named variables (to avoid effectively having to repeat the definition of the named tuple and be robust to ordering of its elements, etc).

… turns out, as often is the case, that someone has already created a package that does almost nearly that! [UnPack.jl](https://github.com/mauro3/UnPack.jl). The only thing it can’t do is let me just unpack the entire named tuple but I take it from another discussion that that is probably not possible anyway:

> [@Unpack named tuples (does anyone know how to do it?)](https://discourse.julialang.org/t/unpack-named-tuples-does-anyone-know-how-to-do-it/32456):
>
> Hi all, I just wanted to know if it is possible to unpack named tuples in the following context. I know that Parameters.jl addresses some related issues, but I have not been able to figure out if it can handle the following case. Define a named tuple using @with\_kw: ParamsNamedTuple = @with\_kw ( T = 1.0, K = 0.5, M = T + K, v = zeros(2), d = 0.1, t = [i \* d for i in 0:10] ) We can unpack particular components of a particular instance using: julia\> params = ParamsNamedTuple(); jul…

… actually, I take it back, it seems there is a way:

> [@With struct do](https://discourse.julialang.org/t/with-struct-do/41341/12):
>
> If you’re willing to explicitly mark which symbols should be treated as fields, then this is easier to implement. Here’s a macro which will replace all expressions of the form &a with container.a: julia\> using MacroTools: postwalk julia\> macro with(container, expr) esc(postwalk(expr) do ex if @capture(ex, &field\_) :(($container).$field) else ex end end) end @with (macro with 1 method) Usage: julia\> mutable st…

This does pretty much exactly what I wanted!

```julia
big_named_tuple_full_of_stuff = (important_var1=42, important_var2=999, irrelevant=-1)

@with big_named_tuple_full_of_stuff begin
  result = &important_var1 + &importantvar_2
end

```

🙂

---

<div class="post-metadata">

**Author:** ![ylxdzsw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ylxdzsw/32/925_2.png) [@ylxdzsw](https://discourse.julialang.org/u/ylxdzsw)\
**Post date:** [December 2, 2020, 6:54am UTC](https://discourse.julialang.org/t/named-tuples-construction-unpacking/51074/2 "2020-12-02T06:54:11Z")

</div>

You can create named tuple by naming none of the elements, by using a semicolon:

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

julia> (a,b)
(1, 2)

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

```

This is documented in [`?NamedTuple`](https://docs.julialang.org/en/v1/base/base/#Core.NamedTuple).

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [December 2, 2020, 8:13am UTC](https://discourse.julialang.org/t/named-tuples-construction-unpacking/51074/3 "2020-12-02T08:13:45Z")

</div>

Nice! I had only looked at [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Named-Tuples), didn’t think to search in the docs for NamedTuple.
