Sunday, November 17, 2024
Google search engine
HomeLanguagesMerging and Updating Dictionary Operators in Python 3.9

Merging and Updating Dictionary Operators in Python 3.9

Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Python. In this article, we will look upon all the old ways of doing these operations as well as about the latest operator released by the Python development team, which will surely be relevant for all the Python programmers.

Using method update(): Update() method is used to merge the second dictionary into the first dictionary without creating any new dictionary and updating the value of the first dictionary, whereas, the value of the second dictionary remains unaltered. Also, the function doesn’t return any value.

Example: 

Python3




# Python code to merge dict
# using update() method
 
 
dict1 = {'a': 10, 'b': 5, 'c': 3}
dict2 = {'d': 6, 'c': 4, 'b': 8}
 
# This return None
print("value returned by update function :",
      dict1.update(dict2))
 
# changes made in dict1
print("dict1 :", dict1)
print("dict2 :", dict2)


Output

value returned by update function : None
dict1 : {'a': 10, 'b': 8, 'c': 4, 'd': 6}
dict2 : {'d': 6, 'c': 4, 'b': 8}

Using ** in Python: There is a trick in Python where we can merge two dictionaries using single expression and store it in a third dictionary. The expression is **. This does not affect the other two dictionaries. ** allows us to pass multiple arguments to a function directly using a dictionary by expanding the contents of the dictionary in the form of collection of key value pairs. For more information refer **kwargs in Python. In this method, we passes all the elements of the all the dictionaries into a new dictionary in a sequential manner. As the dictionaries contents are passed on after another all the values for duplicates keys are overwritten.

Example: 

Python3




# Python code to merge dict
# using a single expression
 
 
dict1 = {'a': 10, 'b': 5, 'c': 3}
dict2 = {'d': 6, 'c': 4, 'b': 8}
 
dict3 = {**dict1, **dict2}
 
print("dict1 :", dict1)
print("dict2 :", dict2)
print("dict3 :", dict3)


Output:

dict1 : {'a': 10, 'b': 5, 'c': 3}
dict2 : {'d': 6, 'c': 4, 'b': 8}
dict3 : {'a': 10, 'b': 8, 'c': 4, 'd': 6}

Using | and |= operators: Dictionary union (|) will return a new dictionary consisting of the left operand merged with the right operand, each of which must be a dictionary. If a key appears in both operands, the last-seen value (i.e. that from the right-hand operand) is selected. Update (|=) operation returns the left operand merged with the right operand.

Example: 

Python3




# Python code to merge dict using
# (|) and (|=) operators
 
dict1 = {'a': 10, 'b': 5, 'c': 3}
dict2 = {'d': 6, 'c': 4, 'b': 8}
 
dict3 = dict1 | dict2
print("Merging dict2 to dict1 (i.e., dict1|dict2) : ")
print(dict3)
 
dict4 = dict2 | dict1
print("\nMerging dict1 to dict2 (i.e., dict2|dict1) : ")
print(dict4)
 
dict1 |= dict2
print("\nMerging dict2 to dict1 and Updating dict1 : ")
print("dict1 : " + dict1)
print("dict2 : " + dict2)


Output:

Merging dict2 to dict1 (i.e. dict1|dict2) : 
{'a': 10, 'd': 6, 'c': 4, 'b': 8}

Merging dict1 to dict2 (i.e. dict2|dict1) : 
{'d': 6, 'a': 10, 'b': 5, 'c': 3}

Merging dict2 to dict1 and Updating dict1 : 
dict1 : {'a': 10, 'd': 6, 'c': 4, 'b': 8}
dict2 : {'d': 6, 'c': 4, 'b': 8}
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments