# Understanding Dict

**URL:** <https://discourse.julialang.org/t/understanding-dict/5602>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 27, 2017, 10:02pm UTC](https://discourse.julialang.org/t/understanding-dict/5602 "2017-08-27T22:02:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 27, 2017, 10:02pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/1 "2017-08-27T22:02:36Z")

</div>

Hi everyone.

I am coming from python background. In python, dictionary keys cannot be mutable because it is based on hash table. However, keys in Dict in julia do not need to be immutable. I wonder how this is achieved?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 27, 2017, 10:14pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/2 "2017-08-27T22:14:33Z")

</div>

mutating keys in a way that changes the hash is undefined.

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [August 28, 2017, 5:35am UTC](https://discourse.julialang.org/t/understanding-dict/5602/3 "2017-08-28T05:35:23Z")

</div>

Translation: Python protects the user from him/herself. Julia lets you shoot yourself in the foot. 😃 (Basically, if a programmer uses a mutable as a key in a Dict, she needs to know to not mutate it in a way that changes the hash, as @yuyichao pointed out.)

Cheers!

Kevin

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 28, 2017, 3:22pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/4 "2017-08-28T15:22:18Z")

</div>

Then why this works?

```julia
julia> d = Dict()
Dict{Any,Any} with 0 entries

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> d[a] = "test"
"test"

julia> a[1] = 4
4

julia> d
Dict{Any,Any} with 1 entry:
  [4, 2, 3] => "test"

```

Hash of an array is not related to its content?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 28, 2017, 3:27pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/5 "2017-08-28T15:27:55Z")

</div>

Here is your test case with one additional instruction to show that `d` is actually broken:

```julia
julia> d = Dict()
Dict{Any,Any} with 0 entries

julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> d[a] = "test"
"test"

julia> a[1] = 4
4

julia> d
Dict{Any,Any} with 1 entry:
  [4, 2, 3] => "test"

julia> d[a]
ERROR: KeyError: key [4, 2, 3] not found
Stacktrace:
 [1] getindex(::Dict{Any,Any}, ::Array{Int64,1}) at .\dict.jl:474

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 28, 2017, 3:29pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/6 "2017-08-28T15:29:40Z")

</div>

It “works” because the table is maintaned and allows `Base.show` to function, but other things break because `d` will be in an inconsistent state. Try eg `d[a]`.

Also, there is a distinction between “not guaranteed to work” and “guaranteed to break”. The latter was not implied.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 28, 2017, 3:35pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/7 "2017-08-28T15:35:25Z")

</div>

> [@Tamas\_Papp](#):
>
> Also, there is a distinction between “not guaranteed to work” and “guaranteed to break”. The latter was not implied.

Right, and that’s what `undefined` means. The language/compiler/whatever can do whatever it want after something [undefined](https://en.wikipedia.org/wiki/Undefined_behavior) happens. It can pretend things still works or it can crash randomly or it can silently fix itself later.

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 28, 2017, 3:49pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/8 "2017-08-28T15:49:02Z")

</div>

Thank you.

I have another question though: Are keys of ObjectIdDict guaranteed not to be garbage collected?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 28, 2017, 4:06pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/9 "2017-08-28T16:06:20Z")

</div>

Anything you can access as an object in julia will not be garbage collected. This certainly include keys of ObjectIdDict but not when you get a pointer to the object by `pointer_from_objref`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [August 28, 2017, 9:03pm UTC](https://discourse.julialang.org/t/understanding-dict/5602/10 "2017-08-28T21:03:25Z")

</div>

> [@Chong\_Wang](#):
>
> In python, dictionary keys cannot be mutable because it is based on hash table

This isn’t true of Python. It will certainly also let you define invalid `hash` functions with the same undefined (and generally memory-safe) behaviors as Julia.
