# Extract elements of NamedTuple

**URL:** <https://discourse.julialang.org/t/extract-elements-of-namedtuple/37731>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 17, 2020, 1:53am UTC](https://discourse.julialang.org/t/extract-elements-of-namedtuple/37731 "2020-04-17T01:53:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jing\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jing_zhang/32/13737_2.png) [@Jing\_Zhang](https://discourse.julialang.org/u/Jing_Zhang)\
**Post date:** [April 17, 2020, 1:53am UTC](https://discourse.julialang.org/t/extract-elements-of-namedtuple/37731/1 "2020-04-17T01:53:05Z")

</div>

Hi Julia users,

I was trying to extract multiple elements from a NamedTuple and Failed.

It works for extracting one element (pt here is a NamedTuple) through the following code line  
pt[:cancer\_type]

But doesn’t work for extracting multiple elements, see below  
pt[:cancer\_type, :time\_to\_cancer\_dx]  
or  
pt[[:cancer\_type, :time\_to\_cancer\_dx]]

Any suggestions? Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [April 17, 2020, 3:03am UTC](https://discourse.julialang.org/t/extract-elements-of-namedtuple/37731/2 "2020-04-17T03:03:55Z")

</div>

```julia
obj = (a = 1, b = 2, c = 3)
a, b, c = obj.a, obj.b, obj.c
a, b, c = [getproperty(obj, pn) for pn in propertynames(obj)]

```

or using Parameters.jl

```julia
using Parameters
@unpack a, b, c = obj

```

Use three back-ticks (`) for formatting code in posts.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 17, 2020, 5:10am UTC](https://discourse.julialang.org/t/extract-elements-of-namedtuple/37731/3 "2020-04-17T05:10:18Z")

</div>

Or something like

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

julia> getproperty.(Ref(nt), [:a, :b])
2-element Array{Int64,1}:
 1
 2

```

but note that a `NamedTuple` may not be the best match for this use case (you don’t get the inference advantages when the field names are provided dynamically, you might as well just use a `Dict` or similar).
