Merge two dictionaries in Julia

Learn the easiest way to merge two dictionaries in Julia.
Published

April 16, 2023

In this article, we’ll explore how to merge or combine two dictionaries in the Julia programming language. Dictionaries are a common and essential data structure, allowing you to store and manipulate key-value pairs efficiently. Combining dictionaries can be a useful operation in various scenarios, such as merging configurations, combining data sets, or aggregating results.

Creating Dictionaries in Julia

Before we dive into merging dictionaries, let’s quickly recap how to create dictionaries in Julia. Dictionaries are created using the Dict keyword, followed by the key-value pairs enclosed in parenthesis and separated by commas. Each key-value pair is connected by an arrow =>.

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

Merging Dictionaries in Julia

There are a few different ways to merge dictionaries in Julia. We will cover three primary methods:

Method 1: Using the splat operator

The splat operator, i.e. ... can be used to splat the keys and values of a dictionary into the Dict constructor:

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

Method 2: Using the merge() function

The merge() function takes two or more dictionaries as arguments and returns a new dictionary containing the merged key-value pairs.

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

Note that this method leaves both dict1 and dict2 unmodified:

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

Method 3: Using the merge!() function

The merge!() function is an in-place version of merge(). Instead of creating a new dictionary, it modifies the first dictionary passed as an argument, adding the key-value pairs from the subsequent dictionaries.

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

Handling Duplicate Keys

When merging dictionaries, it’s important to consider how to handle duplicate keys. By default, the merge() and merge!() functions in Julia will use the value from the last dictionary in the arguments list if a key is present in multiple dictionaries.

If you need to handle duplicate keys differently, you can pass a function as the first argument to merge() or merge!(). This function should accept 2 arguments: the value from the first dictionary, and the value from the second dictionary. The function’s return value will be used as the merged value.

Here’s an example that demonstrates how to use a custom function to merge dictionaries with duplicate keys:

dict1 = Dict("a" => 1, "b" => 2, "c" => 3)
dict2 = Dict("b" => 4, "c" => 5, "d" => 6)

merged_dict = merge(+, dict1, dict2)
Dict{String, Int64} with 4 entries:
  "c" => 8
  "b" => 6
  "a" => 1
  "d" => 6

In this example, the custom function adds the values for duplicate keys.

You can even create a closure that does the custom merge with mergewith:

f = mergewith(+)
f(dict1, dict2)
Dict{String, Int64} with 4 entries:
  "c" => 8
  "b" => 6
  "a" => 1
  "d" => 6

Conclusion

In this blog post, we’ve explored how to merge dictionaries in Julia using the merge() and merge!() functions, as well as how to handle duplicate keys with a custom function. With these tools in your toolbox, you’ll be well-equipped to manipulate dictionaries effectively in your Julia programs.

Reuse

Citation

For attribution, please cite this work as:
“Merge two dictionaries in Julia,” Apr. 16, 2023. https://juliacheat.codes/how-to/merge-two-dictionaries.