Is it safe to pop! a Dict in a loop like this?

GHTaarn
Sat, 06 Jan 2024 15:21:58 +0000
General Usage

At the moment, the following code gives the result that I want (i.e. the for loop goes through each key in the dict once):

let mydict = Dict([i => rand() for i in 1:10])
    println(mydict)
    for k in keys(mydict)
        if isodd(k)
            val = pop!(mydict, k)
            println("$k\t$val")
        end
    end
    println(mydict)
end

Is this something that I can count on in future releases of Julia or would I need to change the “for” line to for k in copy(keys(mydict)) in order to be sure that I would always run the loop once for each key?

16 posts - 4 participants