How to Merge Dictionaries Together in Python



Python


In this article, we show how to merge dictionaries together in Python.

Merging dictionaries together can be useful when you want to do things like search through multiple dictionaries. This method allows you then to search one dictionary as if you were searching all the dictionaries.

We can merge dictionaries together in Python using the update function.

The update() method can merge as many dictionaries together as you want. Each time you want to add another dictionary to the existing dictionary, you specify that dictionary as a parameter to the update function.

We show all of this in the sample code below.



So we have two dictionaries that we created, dict1 and dict2.

We then create another variable, merged, which represents the dictionaries that are merged together.

We first set the merged variable equal to the dictionary, dict1.

We then use the update() function, passing in the parameter of the dictionary we want to add to this merged variable.

Now when we show the contents of the merged variable, we see the contents of dict1 and dict2 merged together.

We then get the contents of all the elements in this merged dictionary.

To show you that an infinite amount of dictionaries can be added using this technique, we add one more dictionary, dict3, to the merged variable. You can now see the contents of the third dictionary added to this merged variable.

Dictionaries with Overlapping Keys

Above is example code where each dictionary had unique keys not seen in any of the other dictionaries.

When we have overlapping keys (which gives us overlapping key-value pairs) and we reference a key in the dictionary, it will return the value of the key of the last merged dictionary.

Let's see below how this works.



So we have 2 dictionaries that both have an 'a' key.

When we merge these dictionaries together, the new merged dictionary is not going to have 2 'a' keys. It's going to have 1 'a' key. Merged dictionaries can only have unique keys, so it cannot have multiple keys that are the same. The value of a key of a merged dictionary will be the value of that key of the last merged dictionaries. In this case, the value of 'a' will be 16, since that is the value of 'a' in the last dictionary that is merged.

Remember that with this update() function to merge dictionaries, we create a completely separate variable from all the original dictionaries. This means if you change a value in the original dictionary after you merged dictionaries, this change will not be present in the new merged dictionary. You would have to merge the dictionaries again if any changes are made to the original dictionaries to have a merged dictionary with the necessary changes.

And this is how we can merge dictionaries in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...