What is the Merge Operator in Python?
A better way to combine dictionaries
Dictionaries are a data type similar to JSON objects. Often referred to as a “dict,” they’re a set of terms (unique identifiers) that each have a definition (value).myDict = {
"first_name": "Jonathan",
"last_name": "Hsu"
}
Since dictionary terms must be unique, there are conflicts if multiple dictionaries have the same term when merging.
In these scenarios, the rule is geberally — last one in wins. In other words, the last dictionary to be merged has priority and will overwrite a term’s value if it’s previously been defined.
Starting from Python version 3.9, the merge operator and its companion, the augmented assignment operator, will become the de facto ways to combine dictionaries.
The Old Way to Merge Dictionaries
The old way to merge dictionaries was through the .update() method or, more recently, the use of the unpacking “splat” operator, both of which are natively available.
The .update() method is called from the primary dictionary and accepts another dictionary as an argument.myDict = { "first_name": "Jonathan", "last_name": "Hsu" }
myDict.update({"first_name": "Jet", "age": 15})print(myDict)
# {'first_name': 'Jet', 'last_name': 'Hsu', 'age': 15}
The original dictionary is modified…an outcome you may not want if you’re trying to keep its original form. To preserve the original using update, you would have to copy the original then proceed with using the .update() method. Too many steps for my liking.
An alternative is the double-asterisk operator, which is used to unpack dictionaries:d1 = { "first_name": "Jonathan", "last_name": "Hsu" }
d2 = { "first_name": "Jet", "age": 15 }
d3 = { **d1, **d2 }print(d3)
# {'first_name': 'Jet', 'last_name': 'Hsu', 'age': 15}
While effective at expressing that all dictionaries are equal and that the last one should win, the code does not read easily. In fact, according to PEP 584, “if you were to ask a typical Python user how to combine two dicts into a new one, I doubt many people would think of {**d1, **d2}.”
Using the Merge Operator
To address both these concerns, the merge operator has been introduced. Using a single vertical bar, the merge operator can be thought of as an addition operator for dictionaries.d1 = { "first_name": "Jonathan", "last_name": "Hsu" }
d2 = { "first_name": "Jet", "age": 15 }
d3 = d1 | d2print(d3)
# {'first_name': 'Jet', 'last_name': 'Hsu', 'age': 15}
As you can see, the merge operator functions identically to the splat technique and does not modify the original dictionary.
But what if we want to modify the original dictionary? In that case, you can use the augmented assignment operator, similar to any other compound assignment operator.myDict = { "first_name": "Jonathan", "last_name": "Hsu" }
myDict |= {"first_name": "Jet", "age": 15}print(myDict)
# {'first_name': 'Jet', 'last_name': 'Hsu', 'age': 15}
In this case, the augmented assignment operator is functionally equivalent to the .update() method but is syntactically similar to the merge operator.
Remember, these operators will only be available in Python 3.9 and above so make sure you add the appropriate version validation to your code.
Thanks for reading, please leave your comments and thoughts below!