# 10 Julia Recipes You Can't Miss

**URL:** <https://discourse.julialang.org/t/10-julia-recipes-you-cant-miss/103298>\
**Category:** Teaching & Outreach\
**Created:** [August 28, 2023, 3:37pm UTC](https://discourse.julialang.org/t/10-julia-recipes-you-cant-miss/103298 "2023-08-28T15:37:49Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 29, 2023, 1:57pm UTC](https://discourse.julialang.org/t/10-julia-recipes-you-cant-miss/103298/13 "2023-08-29T13:57:22Z")

</div>

```julia
julia> abc = (a=1, b="hello", c = 3.14)
(a = 1, b = "hello", c = 3.14)
julia> ac = NamedTuple{(:a, :c)}(abc)
(a = 1, c = 3.14)

```

```julia
"""
    keepkeys(nt::NamedTuple, syms::Tuple{Vararg{Symbol}})

Construct a subset of `nt` using only the keys in `syms`.
""" keepkeys

julia> keepkeys(nt::NamedTuple, ks=NTuple{N,Symbol} where {N}) =
                 NamedTuple{ks}(nt)

julia> b = keepkeys(abc, (:b,))
(b = "hello",)

julia> bc = keepkeys(abc, (:b, :c))
(b = "hello", c = 3.14)

julia> keepkeys(abc, (:z,))
ERROR: type NamedTuple has no field z

```

```julia
"""
    omitkeys(nt::NamedTuple, syms::Tuple{Vararg{Symbol}})

Construct a subset of `nt` omitting the keys in `syms`.
""" omitkeys

julia> omitkeys(nt::NamedTuple, ks=NTuple{N,Symbol} where {N}) =
                  keepkeys(nt, Tuple(setdiff(keys(nt), ks)))

julia> bc = omitkeys(abc, (:a,))
(b = "hello", c = 3.14)

julia> b = omitkeys(abc, (:a, :c))
(b = "hello",)

julia> abc2 = omitkeys(abc, (:z,))
(a = 1, b = "hello", c = 3.14)

```

---

_[View the full topic](https://discourse.julialang.org/t/10-julia-recipes-you-cant-miss/103298)._
