Merge two Dictionaries in swift


In this tutorial, you will see how to merge two dictionaries in Swift programming.

We have already discussed the topic dictionary in Swift. In short, it is a collection type that stores data with key-value pairs.

In Swift, you can find two dedicated methods for merging to dictionaries. One is the dictionary merge() method and the other one is merging().

In this tutorial, you are going to see examples of combining dictionaries using both of these Swift methods.

So let’s continue following this tutorial…

Merge two dictionaries using the merge() method

The merge() method merges the key-value pairs of a dictionary in a given sequence. With this method, you have to use the combining closure that will determine the value for any duplicate keys.

Now see the example below:

var dictionary1 = ["Benjamin" : 1, "Philip" : 2]
var dictionary2 = ["Philip" : 3, "Charlotte" : 4]

dictionary1.merge(dictionary2){(first, _) in first}

print(dictionary1)

Output:

["Charlotte": 4, "Philip": 2, "Benjamin": 1]

In the above example, you can see that the key Philip appears in both of the dictionaries. The dictionary1 and dictionary2 both have the duplicate key Philip. But in the output, you can see the value of the first dictionary taken for the combined array.

The reason behind taking the first one for the duplicate key is that we have used the {(first, _) in first} closure. To get the duplicate key value from the second dictionary, we have to use the {(_, second) in second} just like you can see below:

var dictionary1 = ["Benjamin" : 1, "Philip" : 2]
var dictionary2 = ["Philip" : 3, "Charlotte" : 4]

dictionary1.merge(dictionary2){(_, second) in second}

print(dictionary1)

Output:

["Charlotte": 4, "Philip": 3, "Benjamin": 1]

As you can see in the above output, from the second dictionary dictionary2 the duplicate key Philip has been taken.

Merge two dictionaries using the merging() method

The merging() method in Swift works as same as the merge() method. Both of these methods take exact two parameters just like you can see in the case of the merge() method. It also needs the same combining closure.

The only difference is that the merging method returns a new dictionary. It doesn’t change anything in existing dictionaries. See the example below:

var dictionary1 = ["Benjamin" : 1, "Philip" : 2]
var dictionary2 = ["Philip" : 3, "Charlotte" : 4]

var newDict = dictionary1.merging(dictionary2){(first, _) in first}

// Print the new combined dictionary
print(newDict)

// Print the dictionary one where we apply the merging() method
print(dictionary1)

Output:

["Benjamin": 1, "Philip": 2, "Charlotte": 4]
["Benjamin": 1, "Philip": 2]

In the above example, we can see that we are able to combine two dictionaries. We also tested if the original dictionary changed or not by printing the dictionary1.

In the output, you can see that we have created a new dictionary newDict. We have printed it so that we can verify that the dictionaries are merged and stored in the variable newDict.

In this example, we have used the (first, _) in first closure to get the first one from the duplicate key.

To take the second one from the duplicate, we have to write the code with (_, second) in second closure just like you can see in the example below:

var newDict = dictionary1.merging(dictionary2){(_, second) in second}

The merging() method is useful in the places where you don’t want in-place merging and want to keep the dictionary unchanged.


That’s it…

We have successfully been able to merge two dictionaries in Swift.

A dictionary always can have unique keys. That means duplicate keys can not be present in a dictionary. So the merged dictionary will also have unique keys even though the same key is present in the two dictionaries you are going to merge.