# How to slice a NamedTuple?

**URL:** https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767
**Category:** General Usage
**Tags:** namedtuple
**Created:** [October 25, 2024, 10:53pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767 "2024-10-25T22:53:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [October 25, 2024, 10:53pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/1 "2024-10-25T22:53:50Z")

</div>

How to?

```julia
julia> (;a=10, b=20, c=30)[2:end]
ERROR: MethodError: no method matching getindex(::@NamedTuple{a::Int64, b::Int64, c::Int64}, ::UnitRange{Int64})
# Desired: (;b=20, c=30)

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [October 25, 2024, 11:03pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/2 "2024-10-25T23:03:55Z")

</div>

I think there’s not a super natural operation implemented because it’s type unstable. But you could do

```julia-repl
julia> slice(nt, inds) = NamedTuple{keys(nt)[inds]}(nt)
slice (generic function with 1 method)

julia> nt = (;a=10, b=20, c=30)
(a = 10, b = 20, c = 30)

julia> slice(nt, 2:lastindex(nt))
(b = 20, c = 30)

```

If you wanted the `end` syntax to work here also, you could use `iend` from [GitHub - JuliaArrays/EndpointRanges.jl: Julia package for doing arithmetic on endpoints in array indexing](https://github.com/JuliaArrays/EndpointRanges.jl).

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 25, 2024, 11:10pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/3 "2024-10-25T23:10:54Z")

</div>

> [@ericphanson](#):
>
> `NamedTuple{keys(nt)[inds]}(nt)`

Or even simpler, `nt[keys(nt)[inds]]`.  
NamedTuples are indexable with multiple symbols, but not with multiple integers…

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [October 25, 2024, 11:13pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/4 "2024-10-25T23:13:13Z")

</div>

ah this is nicer too since you can use `end`:

```julia
julia> nt[keys(nt)[2:end]]
(b = 20, c = 30)

```

I guess if we support indexing by symbols dynamically then there’s not really a good reason to not support integers.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 25, 2024, 11:15pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/5 "2024-10-25T23:15:10Z")

</div>

Btw, if you really need to drop just the first/last element and happen to already be using Accessors, then

```julia
julia> using Accessors

julia> @delete first(nt)
(b = 20, c = 30)

```

is readable and type-stable 🙂

---

<div class="post-metadata">

### Author: ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)
#### Post date: [October 26, 2024, 12:56am UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/6 "2024-10-26T00:56:33Z")

</div>

Also, using `rest()`:

```julia
julia> nt = (;a=10, b=20, c=30)
(a = 10, b = 20, c = 30)

julia> Base.rest(nt, 2)
(b = 20, c = 30)

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 28, 2024, 5:14pm UTC](https://discourse.julialang.org/t/how-to-slice-a-namedtuple/121767/7 "2024-10-28T17:14:38Z")

</div>

> [@lbilli](#):
>
> `Base.rest(nt, 2)`

Nice as well, but note that it’s not type-stable, and relies on internals (meaning of the 2nd argument is not specified).
