# In operator for set of sets

**URL:** <https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474>\
**Category:** New to Julia\
**Tags:** question, set\
**Created:** [November 20, 2020, 3:49am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474 "2020-11-20T03:49:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsuwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsuwal/32/19577_2.png) [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Post date:** [November 20, 2020, 3:49am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/1 "2020-11-20T03:49:10Z")

</div>

![Screen Shot 2020-11-19 at 10.46.02 PM](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8d42e968b7a41361ad47f3128a0cecd3eb257e8.png)

Can I get some help with explaining this behaviour?

At first it seems that you can find the existence of a set within a set of sets, but after I make a modification to the set I cannot do so?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 20, 2020, 4:11am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/2 "2020-11-20T04:11:46Z")

</div>

ok so I think the root cause is `s.dict.slots` is not updated due to the way you deleted an element in the keys of `s.dict`.

This is not limited to Set of Sets, you can reproduce this behavior even with a Set of Arrays

```julia
julia> s = Set([[1,2,3]])
Set{Array{Int64,1}} with 1 element:
  [1, 2, 3]

julia> [1,2,3] ∈ s
true

julia> deleteat!(only(s), 2)
2-element Array{Int64,1}:
 1
 3

julia> s
Set{Array{Int64,1}} with 1 element:
  [1, 3]

julia> [1,2,3] ∈ s
false

julia> [1,3] ∈ s
false

```

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 20, 2020, 4:47am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/3 "2020-11-20T04:47:01Z")

</div>

The problem is that `Set` is implemented as a `Dict` which in turn is backed by a hashtable using a combination of == and ===. After you `delete!` 3 from `comp`, the key is mutated to `Set([4])`, but it’s still stored where the hash function would expect to find `Set[3,4]`.

I think what you want is

```julia
for comp in s
    delete!(s,comp)
    push!(s,delete!(comp,3))
end

```

i.e. remove the unmutated `Set` from the dictionary first, and replace it with the mutated `Set`.

This is a case of the more general problem of what happens when dictionary keys are mutable.  
[https://github.com/JuliaLang/julia/issues/33176](https://github.com/JuliaLang/julia/issues/33176)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 20, 2020, 11:03am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/4 "2020-11-20T11:03:39Z")

</div>

I think this is a bug, because the docstring for `in` says that it compares via `isequal` for `Set`s, and `isequal(Set([4]), first(s))` is `true` in the example.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [November 20, 2020, 11:38am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/5 "2020-11-20T11:38:29Z")

</div>

> [@ericphanson](#):
>
> I think this is a bug, because the docstring for `in` says that it compares via `isequal` for `Set` s

There is not really a way around this behavior though: objects stored in a `Dict` or `Set` must not be mutated in a way that changes their hash, otherwise all the functions which use it might fail. So rather than a bug, it could simply be a documentation issue.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 20, 2020, 11:40am UTC](https://discourse.julialang.org/t/in-operator-for-set-of-sets/50474/6 "2020-11-20T11:40:53Z")

</div>

> [@rfourquet](#):
>
> So rather than a bug, it could simply be a documentation issue.

Makes sense 🙂. I’d still call it a bug (albeit a docs bug), because the semantics one can infer from the docs does not match what actually happens.
