julia
julia copied to clipboard
More precision on missing with Dicts
The statement made here about the in
operator with Dicts
is too strong and it has edge cases where it fails:
Some collections follow a slightly different definition. For example, Sets check whether the item isequal to one of the elements. Dicts look for key=>value pairs, and the key is compared using isequal. To test for the presence of a key in a dictionary, use haskey or k in keys(dict). For these collections, the result is always a Bool and never missing.
On Sets
the above is utterly true, but the statement fails when the value
in key=>value in Dict
is missing
:
julia> X = Dict(1=>10, 2=>20, 3=>30, missing=>NaN);
julia> (missing=>10) in X
false
julia> (missing=>NaN) ∈ X
false
julia> (1=>missing) ∈ X
missing
julia> (missing=>missing) ∈ X
missing
As the above shows, for Dict
s, a Bool
is not always returned. So its best to state this out.
Moreover, since only the key
is compared using isequal
for Dicts
. key should be “key
(not the value
)” so emphasis is seen as being placed on key
only.