# Apply dictionary (Dict) to many keys at once (broadcast)

**URL:** https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674
**Category:** General Usage
**Tags:** question, broadcast, dictionary
**Created:** [October 13, 2022, 3:59pm UTC](https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674 "2022-10-13T15:59:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dorn-gerhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dorn-gerhard/32/27153_2.png) [@dorn-gerhard](https://discourse.julialang.org/u/dorn-gerhard)
#### Post date: [October 13, 2022, 3:59pm UTC](https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674/1 "2022-10-13T15:59:33Z")

</div>

Given a simple dictionary `d = Dict(1 => "a", 2 => "b", 3 => "c")`

Is there a simple way to apply the dictionary to a list / vector / tuple of keys at once?

I would like to “translate” a vector of numbers into letters like  
`p = [1 3 2 1 2 1 3]`

my current solutions include:

1. using the **get** function: `get.(Ref(d), p, 0)`
2. using a **map** function:

```julia
map(p) do x
  d[x]
end

```

1. using a **comprehension**  
`[d[x] for x in p]`
2. or writing an **eval function** (for short hand notation)  
`dict_eval(dict, key) = get.(Ref(dict), key, 0)`  
using  
`dict_eval(d,p)`

Do you have a suggestions for a better (short and fast = elegant) solution?

Concerning the syntax: My intuition would say that I would broadcast a dictionary to a vector of keys with a syntax like  
`d.[p]`  
but this is not (yet) a valid syntax in Julia.

Maybe this relates to the [github issue 30836](https://github.com/JuliaLang/julia/issues/30836)?

I am looking for a short syntax as this would help a lot in applying multiple dictionaries sequentially at once like (just think of encrypting text with different dictionaries like Caesar cypher)  
`caesar = Dict("a" => "d", "b" => "e", "c" => "f")`

`caesar.[d.[p]]`

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 13, 2022, 9:39pm UTC](https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674/3 "2022-10-13T21:39:27Z")

</div>

Dictionaries can readily be considered as functions with a finite domain. Thus, arguably, they should be callable anyways:

```julia
(d::Dict)(k) = d[k]

```

and `caesar.(d.(p))` just works as expected.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 13, 2022, 9:49pm UTC](https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674/4 "2022-10-13T21:49:13Z")

</div>

This is so useful.  
Why aren’t the Dicts shipped like this from the factory?

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 13, 2022, 9:51pm UTC](https://discourse.julialang.org/t/apply-dictionary-dict-to-many-keys-at-once-broadcast/88674/5 "2022-10-13T21:51:15Z")

</div>

That’s exactly what I thought about any other language after seeing that dictionaries and vectors are callable in Clojure. It just feels correct.
