# Dicts with more than one key?

**URL:** https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680
**Category:** General Usage
**Tags:** dictionary
**Created:** [October 13, 2022, 4:41pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680 "2022-10-13T16:41:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 13, 2022, 4:41pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/1 "2022-10-13T16:41:59Z")

</div>

Is there a standard way to create a `Dict` with more than one key? Or one needs to unavoidably create multiple Dicts by hand?

Something like (I’m inventing here the notation):

```julia
letters = Dict{Keys{Char,Char},String}(
    Keys('a','A') => "This is letter A",
    Keys('b','B') => "This is letter B",
)

julia> letters['a']
This is letter A

julia> letters['A']
This is letter A

etc.

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 13, 2022, 4:47pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/2 "2022-10-13T16:47:05Z")

</div>

Not that I know of? Of course, you can simply add multiple keys with the same value to a single dictionary, ala `s = "This is letter A"; letters['a'] = letters['A'] = s`.

It seems better to have a function that canonicalizes your keys before you pass them to the dictionary, or similarly have a custom hashing and equality function. In your example, it is as simple as `letters[lowercase(c)]`

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [October 13, 2022, 4:47pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/3 "2022-10-13T16:47:45Z")

</div>

I don’t think there’s a standard way to do that. If you control the type that is used for the keys, you could ensure that seemingly different keys hash the same. For example, you could make a string macro `k"a"` that produces a `Key("a")`, and then you can define `hash` in such a way that `hash(Key("a")) == hash(Key("A"))`.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 13, 2022, 4:51pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/4 "2022-10-13T16:51:13Z")

</div>

> [@CameronBieganek](#):
>
> `hash(Key("a")) == hash(Key("b"))`.

> [@stevengj](#):
>
> In your example, it is as simple as `letters[lowercase(c)]`

Yeah, something like those ideas is simple enough. 🙂

To make that completely hidden one could create a new type of Dict and overwrite getindex, I guess, but in my case it is an internal convenience, so not worth the effort.

Thanks both!

---

<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, 6:43pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/5 "2022-10-13T18:43:54Z")

</div>

This looks close enough to your wished syntax:

```julia
letters = Dict([['a','A'] .=> "This is letter A";
                ['b','B'] .=> "This is letter B"])

letters['a'] # "This is letter A"
letters['A'] # "This is letter A"

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 13, 2022, 7:22pm UTC](https://discourse.julialang.org/t/dicts-with-more-than-one-key/88680/6 "2022-10-13T19:22:59Z")

</div>

🙂 Changing the solution! That is really simple and concise.
