merge!

merge!(collection, others...)

Update collection with pairs from the other collections

Examples

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

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

julia> merge!(a, b);

julia> a
Dict{UTF8String,Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
julia> a = Dict("foo" => 0.0, "bar" => 42.0)
Dict{ASCIIString,Float64} with 2 entries:
  "bar" => 42.0
  "foo" => 0.0

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

julia> merge!(a, b);

julia> a
Dict{UTF8String,Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
  1. Merge dictionaries:

    julia> dict1 = Dict("a" => 1, "b" => 2);
    julia> dict2 = Dict("b" => 3, "c" => 4);
    julia> merge!(dict1, dict2)
    Dict{String,Int64} with 3 entries:
     "a" => 1
     "b" => 3
     "c" => 4

    This example merges the key-value pairs from dict2 into dict1. If a key already exists in dict1, its value is updated with the value from dict2.

  2. Merge arrays:

    julia> arr1 = [1, 2, 3];
    julia> arr2 = [4, 5];
    julia> merge!(arr1, arr2)
    5-element Array{Int64,1}:
    1
    2
    3
    4
    5

    The merge! function can also be used to merge arrays. It appends the elements from arr2 to arr1.

  3. Merge multiple dictionaries:
    julia> dict1 = Dict("a" => 1, "b" => 2);
    julia> dict2 = Dict("b" => 3, "c" => 4);
    julia> dict3 = Dict("d" => 5);
    julia> merge!(dict1, dict2, dict3)
    Dict{String,Int64} with 4 entries:
     "a" => 1
     "b" => 3
     "c" => 4
     "d" => 5

    The merge! function can accept multiple dictionaries as arguments. It merges all the key-value pairs from the provided dictionaries into the first dictionary.

Common mistake example:

julia> arr1 = [1, 2, 3];
julia> arr2 = 4;
julia> merge!(arr1, arr2)
ERROR: MethodError: no method matching merge!(::Array{Int64,1}, ::Int64)

In this example, the second argument arr2 is not a collection (array or dictionary), which causes a MethodError. Ensure that the merge! function is used with compatible collection types.

See Also

append!, delete!, deleteat!, empty!, endof, filter, filter!, gc, get!, getkey, haskey, insert!, isempty, keys, map, map!, merge, merge!, pop!, prepend!, push!, reduce, resize!, shift!, splice!, unshift!, values,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: