Different modification behavior when looping over array of variables of different array types

chaseleslie
Wed, 10 Sep 2025 00:39:47 +0000
General Usage

Example code:

function filterarrays!(d)
    for arr in [d["a"], d["b"]]
        deleteat!(arr, 2)
    end
end

d = Dict("a" => [1, 2, 3], "b" => [1., 2., 3.])
display(d)
filterarrays!(d)
display(d)

With output:

Dict{String, Vector} with 2 entries:
  "b" => [1.0, 2.0, 3.0]
  "a" => [1, 2, 3]
Dict{String, Vector} with 2 entries:
  "b" => [1.0, 3.0]
  "a" => [1, 2, 3]

In the case of the b key of dictionary d, the middle array element is removed as expected. For the values of the a key, no elements are removed. I’m not sure if the reason for the difference is related to this topic, but in the example above the two arrays have different types and the output for the Float64 array (b) is as I would expect.

Can anyone explain why the loop behavior is different here?

(Julia Version 1.11.6)

9 posts - 4 participants