# Dict with Float64 and Int keys of the same value?

**URL:** <https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044>\
**Category:** General Usage\
**Created:** [September 22, 2017, 9:16pm UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044 "2017-09-22T21:16:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 22, 2017, 9:16pm UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044/1 "2017-09-22T21:16:34Z")

</div>

```julia
d = Dict()
d[1.0] = "hello"
d[1] = "goodbye"
print(d)
# ==> 
# Dict{Any,Any} with 1 entry:
# 1 => "goodbye

```

So `1.0` and `1` are treated as the same key. Although this behavior makes sense in some contexts, I wonder if there’s a way to have both keys with different values?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 22, 2017, 11:05pm UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044/2 "2017-09-22T23:05:35Z")

</div>

`ObjectIdDict` keys objects on `===` (i.e. identity).

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 22, 2017, 11:15pm UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044/3 "2017-09-22T23:15:01Z")

</div>

Thanks, indeed this works `Float64` vs `Int` case. But using `ObjectIdDict` also has an unfortunate side effect of not finding keys of other types:

```julia
d = ObjectIdDict()
d[1.0] = "hello"
d[1] = "goodbye"
print(d)
# ObjectIdDict with 2 entries:
# 1 => "goodbye"
# 1.0 => "hello"
# good!

d["x"] = "bonjour"
d["x"]
# ERROR: KeyError: key "x" not found

```

Any other option that also preserves behavior for non-numeric types?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 22, 2017, 11:16pm UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044/4 "2017-09-22T23:16:13Z")

</div>

That is fixed on master since `String` is now officially an immutable type and `"hello" === "hello"` unlike on 0.6. On 0.5 (or otherwise), if you need some custom hybrid behavior, normalize the keys before inserting or looking up.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 23, 2017, 12:10am UTC](https://discourse.julialang.org/t/dict-with-float64-and-int-keys-of-the-same-value/6044/5 "2017-09-23T00:10:23Z")

</div>

> [@StefanKarpinski](#):
>
> if you need some custom hybrid behavior, normalized the keys before inserting or looking up.

Thanks, using `string(key)` instead of actual key worked for me.
