Broadcast [possibly missing] key value pairs from one Dict to another?

How can I broadcast key value pairs from one Dict to another, whether or not they exist?

Tried the following but it doesn’t work:

check = Dict()
check2 = Dict(:a=>1,:b=>"b")
check[keys(check2).=values(check2)]
@test get(check,:a,"") == 1 && get(check,:b,"") == "b"

ERROR: KeyError: key Pair{Symbol,B} where B[:a => 1, :b => "b"] not found
Stacktrace:
 [1] getindex(::Dict{Any,Any}, ::Array{Pair{Symbol,B} where B,1}) at ./dict.jl:467
 [2] top-level scope at /home/au/code/BioPAXwork/local/pcquery/Ex_annotate_seurat.jl:71

Broadcasting seems readable and efficient by if there is a better way to update one Dict with another that’s great too.

You can’t broadcast per-se but you can very easily do something like

[check[k] = v for (k,v) in check2]

It’s not clear though whether your question matches the example you’ve written, so I’m not sure if the above actually helps…

Edit:
You can also use merge and union (and their ! counterparts)

3 Likes

perfect, thanks