In Julia, how to merge a dictionary?
Asked Answered
B

3

23

What is the best way to merge a dictionary in Julia?

> dict1 = Dict("a" => 1, "b" => 2, "c" => 3)
> dict2 = Dict("d" => 4, "e" => 5, "f" => 6)
# merge both dicts
> dict3 = dict1 with dict2
> dict3
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4
Bureaucratize answered 26/3, 2016 at 20:34 Comment(0)
C
31

https://docs.julialang.org/en/latest/base/collections/#Base.merge

merge(collection, others...)

Construct a merged collection from the given collections. If necessary, the types of the resulting collection will be promoted to accommodate the types of the merged collections. If the same key is present in another collection, the value for that key will be the value it has in the last collection listed.

julia> merge(dict1,dict2)
    Dict{ASCIIString,Int64} with 6 entries:
      "f" => 6
      "c" => 3
      "e" => 5
      "b" => 2
      "a" => 1
      "d" => 4

merge!(collection, others...)
Update collection with pairs from the other collections.

julia> merge!(dict1,dict2)
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4

julia> dict1
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4
Cushing answered 26/3, 2016 at 20:54 Comment(2)
Just out if curiosity, how would thus handle conflicting key values? If I have values 'a =5' in 'dict1' and 'a =7' in 'dict2' what would the value of 'a' be in the resulting dictionary?Epp
If you wanted to keep the conflicting values, you could use union(dict1, dict2); however, this would return an Array rather than a Dict.Verve
S
10

You can use merge. If the Dicts have elements with the same key, the value for that key will be of the last Dict listed.

If you want to combine the elements of the Dicts that have the same key, you can use mergewith(combine, collection, others...). combine is a function that receive two values and return a value. See mergewith.

Example of the docs:

julia> a = Dict("foo" => 0.0, "bar" => 42.0)
Dict{String,Float64} with 2 entries:
  "bar" => 42.0
  "foo" => 0.0

julia> b = Dict("baz" => 17, "bar" => 4711)
Dict{String,Int64} with 2 entries:
  "bar" => 4711
  "baz" => 17

julia> mergewith(+, a, b)
Dict{String,Float64} with 3 entries:
  "bar" => 4753.0
  "baz" => 17.0
  "foo" => 0.0
Slackjawed answered 12/6, 2020 at 0:56 Comment(2)
So if I would like to keep the second element, can I use merge((x, y) -> y, a, b)?Trifolium
I think this would work, but you can use merge(a, b).Slackjawed
B
3

You can use the ... operator, like this:

julia> dict1 = Dict("a" => 1, "b" => 2, "c" => 3);

julia> dict2 = Dict("d" => 4, "e" => 5, "f" => 6);

julia> Dict(dict1..., dict2...)
Dict{String, Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4
Bays answered 7/11, 2022 at 16:57 Comment(1)
Neat! Works for Vectors and Sets too.Trifolium

© 2022 - 2024 — McMap. All rights reserved.