# Is there a good implementation of a BiDict (invertable Dict)

**URL:** <https://discourse.julialang.org/t/is-there-a-good-implementation-of-a-bidict-invertable-dict/76789>\
**Category:** General Usage\
**Tags:** question, data\_structures\
**Created:** [February 20, 2022, 9:46am UTC](https://discourse.julialang.org/t/is-there-a-good-implementation-of-a-bidict-invertable-dict/76789 "2022-02-20T09:46:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 20, 2022, 9:46am UTC](https://discourse.julialang.org/t/is-there-a-good-implementation-of-a-bidict-invertable-dict/76789/1 "2022-02-20T09:46:31Z")

</div>

I need a lookup table that works in both directions for saving and loading a custom file.  
Therefore I thought a dictionary that also used the values as keys might be handy.

Is there a good implementation for this?

My current attempt looks like this:

```julia
struct BiDict
    key1        
    key2
end

function Base.getindex(bd::BiDict,::Val{1},ind)
    bd.key2[findfirst(isequal(ind),bd.key1)]
end

function Base.getindex(bd::BiDict,::Val{2},ind)
    bd.key1[findfirst(isequal(ind),bd.key2)]
end

```

It can be used to search in both directions using the first index, although I am not sure how to best avoid using `Val`.

```julia
julia> lookup = BiDict([2,4,3,1],[:s,:j,:e,:t])
BiDict([2, 4, 3, 1], [:s, :j, :e, :t])

julia> lookup[Val(1),4]
:j

julia> lookup[Val(2),:j]
4

```

---

<div class="post-metadata">

**Author:** ![djholiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djholiver/32/50470_2.png) [@djholiver](https://discourse.julialang.org/u/djholiver)\
**Post date:** [February 20, 2022, 10:14am UTC](https://discourse.julialang.org/t/is-there-a-good-implementation-of-a-bidict-invertable-dict/76789/2 "2022-02-20T10:14:18Z")

</div>

> [@Dictionary-like data structure representing a bijective map?](https://discourse.julialang.org/t/dictionary-like-data-structure-representing-a-bijective-map/45341/2):
>
> I found one! [https://github.com/scheinerman/Bijections.jl](https://github.com/scheinerman/Bijections.jl) For reference, here’s the syntax used by Bijections.jl: julia\> b = Bijection(:a, 1) Bijection{Symbol,Int64} (with 1 pairs) julia\> b[:a] 1 julia\> b(1) :a

Might be what you’re after

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 20, 2022, 10:33am UTC](https://discourse.julialang.org/t/is-there-a-good-implementation-of-a-bidict-invertable-dict/76789/3 "2022-02-20T10:33:43Z")

</div>

This looks good. Thank you!  
I am just wondering if it really is necessary to store two sets as well as two dicts to do this.
