<p dir="auto">I think it would be useful to have some syntax for returning a subset of a <code class="notranslate">NamedTuple</code> as another <code class="notranslate">NamedTuple</code>, analogous to the same ability with regular <code class="notranslate">Tuple</code>s.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; tup = (1, 2, 3)
(1, 2, 3)

julia&gt; tup[1]
1

julia&gt; tup[[1,2]]
(1, 2)

julia&gt; tup[[1]]
(1,)

julia&gt; nt = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)

julia&gt; nt[:a]
1

julia&gt; nt[[:a, :b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b, :c), Tuple{Int64, Int64, Int64}}, ::Vector{Symbol})"><pre class="notranslate"><code class="notranslate">julia&gt; tup = (1, 2, 3)
(1, 2, 3)

julia&gt; tup[1]
1

julia&gt; tup[[1,2]]
(1, 2)

julia&gt; tup[[1]]
(1,)

julia&gt; nt = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)

julia&gt; nt[:a]
1

julia&gt; nt[[:a, :b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b, :c), Tuple{Int64, Int64, Int64}}, ::Vector{Symbol})
</code></pre></div>
<p dir="auto">The following works, though I don't know if it's the most efficient:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; get(n::NamedTuple, ntkeys) = (; (s=&gt;n[s] for s in ntkeys)...)
get (generic function with 2 methods)

julia&gt; get(nt, [:a,:b])
(a = 1, b = 2)

julia&gt; get(nt, [:a])
(a = 1,)"><pre class="notranslate"><code class="notranslate">julia&gt; get(n::NamedTuple, ntkeys) = (; (s=&gt;n[s] for s in ntkeys)...)
get (generic function with 2 methods)

julia&gt; get(nt, [:a,:b])
(a = 1, b = 2)

julia&gt; get(nt, [:a])
(a = 1,)
</code></pre></div>
<p dir="auto">I'm not advocating it actually be called <code class="notranslate">get</code> - I think something that worked with <code class="notranslate">getindex</code> would be ideal. NamedTupleTools.jl <a href="https://github.com/JeffreySarnoff/NamedTupleTools.jl/blob/master/src/NamedTupleTools.jl#L260-L270">has a method called <code class="notranslate">select</code></a> that does this, but it seems like this functionality would be great to have in Base.</p>
<p dir="auto">One counter argument is that this doesn't work with <code class="notranslate">Dict</code>s</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; dict = Dict(pairs(nt)...)
Dict{Symbol, Int64} with 3 entries:
  :a =&gt; 1
  :b =&gt; 2
  :c =&gt; 3

julia&gt; dict[[:a, :b]]
ERROR: KeyError: key [:a, :b] not found"><pre class="notranslate"><code class="notranslate">julia&gt; dict = Dict(pairs(nt)...)
Dict{Symbol, Int64} with 3 entries:
  :a =&gt; 1
  :b =&gt; 2
  :c =&gt; 3

julia&gt; dict[[:a, :b]]
ERROR: KeyError: key [:a, :b] not found
</code></pre></div>
<p dir="auto">But it is technically possible to have an array as a key in a <code class="notranslate">Dict</code>, but not for a <code class="notranslate">NamedTuple</code>.</p>
<p dir="auto">I'm happy to make a PR if this idea is acceptable, and if there aren't any implications that are escaping me.</p>
<p dir="auto">Couldn't find an existing issue that quite addressed this, apologize if dup (or naïve).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; versioninfo()
Julia Version 0.7.0-DEV.5008
Commit 2cab814429 (2018-05-05 14:43 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin17.5.0)
  CPU: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
Environment:
  JULIA_REVISE = manual

julia&gt; x = (1, 2); y = (a = 1, b = 2);

julia&gt; x[1:2]
(1, 2)

julia&gt; y[1]
1

julia&gt; y[2]
2

julia&gt; y[1:2]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::UnitRange{Int64})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101

julia&gt; y[[:a, :b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::Array{Symbol,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101"><pre class="notranslate"><code class="notranslate">julia&gt; versioninfo()
Julia Version 0.7.0-DEV.5008
Commit 2cab814429 (2018-05-05 14:43 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin17.5.0)
  CPU: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
Environment:
  JULIA_REVISE = manual

julia&gt; x = (1, 2); y = (a = 1, b = 2);

julia&gt; x[1:2]
(1, 2)

julia&gt; y[1]
1

julia&gt; y[2]
2

julia&gt; y[1:2]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::UnitRange{Int64})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101

julia&gt; y[[:a, :b]]
ERROR: MethodError: no method matching getindex(::NamedTuple{(:a, :b),Tuple{Int64,Int64}}, ::Array{Symbol,1})
Closest candidates are:
  getindex(::NamedTuple, ::Int64) at namedtuple.jl:100
  getindex(::NamedTuple, ::Symbol) at namedtuple.jl:101
</code></pre></div>
<p dir="auto">It just might be nice to have these indexing methods at some point down the road.</p>
<p dir="auto">Thoughts?</p>