# Unexpected behavior of loop over set

**URL:** <https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371>\
**Category:** General Usage\
**Tags:** question, bug, loops, set\
**Created:** [September 29, 2023, 12:33am UTC](https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371 "2023-09-29T00:33:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [September 29, 2023, 12:33am UTC](https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371/1 "2023-09-29T00:33:12Z")

</div>

I have the following (minimized, but perhaps not minimal) code snippet which is intended to group nodes of graph components. The surprising thing is that it seems to imply that elements of a set are not in the set.

```julia
ns = [1,2,5,4,3]
cs = Set()
for n in ns
    x = nothing
    for c in cs
        if !(c in cs)
            println(c, " is not in ", cs)
        end
        for m in c
            if abs(m-n) <=1

                if isnothing(x)
                    push!(c,n)
                    x = c
                else
                    union!(x,c)
                    delete!(cs,c)
                end
                break
            end
        end
    end
    if isnothing(x)
        s = Set()
        push!(s,n)
        push!(cs,s)
    end
end
cs

```

This outputs

```julia
Set(Any[2, 1]) is not in Set(Any[Set(Any[2, 1])])
Set(Any[2, 1]) is not in Set(Any[Set(Any[5, 4]), Set(Any[2, 1])])
Set(Any[5, 4]) is not in Set(Any[Set(Any[5, 4]), Set(Any[2, 1])])
Set(Any[2, 1]) is not in Set(Any[Set(Any[5, 4, 3]), Set(Any[2, 1])])

```

Can someone help me understand what is going on here? Is this some scoping or evaluation order issue?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 29, 2023, 1:34am UTC](https://discourse.julialang.org/t/unexpected-behavior-of-loop-over-set/104371/2 "2023-09-29T01:34:09Z")

</div>

You’re doing two often discouraged things, 1) removing or adding elements to a container while iterating it `delete!(cs,c)`, and 2) mutating the keys of a hash table `push!(c,n)`. The 2nd one is why you’re seeing this apparent paradox; a mutated key’s hash value is _not_ updated, so the hash value computed from its new value will differ and fail to find the entry. A much simpler example:

```julia
julia> s = Set([[1]]) # [1] entry hashed here
Set{Vector{Int64}} with 1 element:
  [1]

julia> s = Set([[1]]);

julia> for c in s println(c in s) end
true

julia> for c in s push!(c, 10) end

julia> s # [1, 10] entry still has hash from [1]
Set{Vector{Int64}} with 1 element:
  [1, 10]

julia> for c in s println(c in s) end
false

```
