# Is there something like the get() function for namedTuples?

**URL:** <https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463>\
**Category:** General Usage\
**Tags:** dictionary, namedtuple\
**Created:** [January 3, 2023, 5:34pm UTC](https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463 "2023-01-03T17:34:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)\
**Post date:** [January 3, 2023, 5:34pm UTC](https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463/1 "2023-01-03T17:34:29Z")

</div>

Hello,  
Is there an equivalent of the get() function for dictionaries for NamedTuples?  
I want to get the value for the ‘key’ of a NamedTuple if it exists, and if it does not assign it to some default value; pretty much exactly what the get() function does with dictionaries.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 3, 2023, 5:40pm UTC](https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463/2 "2023-01-03T17:40:55Z")

</div>

This already works (since v1.7):

```julia
julia> get((a = 2, b = 3), :c, 4)
4

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 3, 2023, 8:48pm UTC](https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463/4 "2023-01-03T20:48:01Z")

</div>

```julia
help?> get
search: get get! getpid getkey getfield getindex getglobal getproperty gethostname get_zero_subnormals

  get(collection, key, default)

  Return the value stored for the given key, or the given default value if no mapping for the key is present.

  │ Julia 1.7
  │
  │ For tuples and numbers, this function requires at least Julia 1.7.

```

---

<div class="post-metadata">

**Author:** ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)\
**Post date:** [January 4, 2023, 10:50am UTC](https://discourse.julialang.org/t/is-there-something-like-the-get-function-for-namedtuples/92463/5 "2023-01-04T10:50:18Z")

</div>

Thank you! I was trying it as `"c"` instead of `:c` .
