# How can I access multiple values of a dictionary using a tuple of keys?

**URL:** <https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868>\
**Category:** General Usage\
**Created:** [March 10, 2021, 11:03am UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868 "2021-03-10T11:03:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 10, 2021, 11:03am UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/1 "2021-03-10T11:03:22Z")

</div>

How can I access multiple values of a dictionary using a tuple of keys?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1615373523265600?thread_ts=1615373523.265600&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![mroavi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mroavi/32/8804_2.png) [@mroavi](https://discourse.julialang.org/u/mroavi)\
**Post date:** [March 10, 2021, 11:04am UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/2 "2021-03-10T11:04:19Z")

</div>

I found this other post, but it doesn’t seem to work in Julia 1.5

> [@Accessing multiple values of a dictionary](https://discourse.julialang.org/t/accessing-multiple-values-of-a-dictionary/8648/2):
>
> [https://github.com/JuliaLang/julia/issues/19169](https://github.com/JuliaLang/julia/issues/19169) There are two fairly compelling potential meanings of x.[y]. It’s not yet clear which one is a better choice, and we’re not really considering adding new features until 1.0 is well out the door.

---

<div class="post-metadata">

**Author:** ![mroavi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mroavi/32/8804_2.png) [@mroavi](https://discourse.julialang.org/u/mroavi)\
**Post date:** [March 10, 2021, 11:20am UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/3 "2021-03-10T11:20:36Z")

</div>

By @pfitzseb:

> I’d just broadcast `getindex` :
> 
> ```julia
> julia> d = Dict(:a => 1, :b => 2, :c => 3)
> Dict{Symbol,Int64} with 3 entries:
> :a => 1
> :b => 2
> :c => 3julia> getindex.(Ref(d), (:a, :b, :c))
> (1, 2, 3)
> 
> ```

The `Ref(...)` does the trick.

---

<div class="post-metadata">

**Author:** ![vancleve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vancleve/32/4183_2.png) [@vancleve](https://discourse.julialang.org/u/vancleve)\
**Post date:** [June 16, 2021, 3:28pm UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/4 "2021-06-16T15:28:07Z")

</div>

Do you know what the `Ref` is necessary?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 16, 2021, 4:16pm UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/5 "2021-06-16T16:16:35Z")

</div>

The `Ref` is necessary for the `Dict` to be treated as an scalar by the broadcast instead of a collection. Wrapping in an unitary tuple would have the same effect: `getindex.((d,), (:a, :b, :c))`.

---

<div class="post-metadata">

**Author:** ![jakewilliami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakewilliami/32/18945_2.png) [@jakewilliami](https://discourse.julialang.org/u/jakewilliami)\
**Post date:** [September 22, 2021, 10:50am UTC](https://discourse.julialang.org/t/how-can-i-access-multiple-values-of-a-dictionary-using-a-tuple-of-keys/56868/6 "2021-09-22T10:50:44Z")

</div>

I have found the following to benchmark slightly faster than the `Ref` method:

```julia
tuple((d[i] for i in (:a, :b, :c))...)

```

This has the added bonus of also not needing the `tuple` or splatting (`...`) if you are only iterating through results anyway (or replacing `tuple` with another function). With the `tuple` and the splatting, this will produce the same result as the `Ref` method.
