Open In App

Merging and Updating Dictionary Operators in Python 3.9

Last Updated : 03 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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}


Previous Article
Next Article

Similar Reads

Python | Updating value list in dictionary
While working with dictionary with list as value is always vulnerable of getting it's value updated. The ways or shorthands to perform this task can be handy in such situations. This can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension The naive method to perform t
7 min read
Creating and updating PowerPoint Presentations in Python using python - pptx
python-pptx is library used to create/edit a PowerPoint (.pptx) files. This won't work on MS office 2003 and previous versions. We can add shapes, paragraphs, texts and slides and much more thing using this library. Installation: Open the command prompt on your system and write given below command: pip install python-pptx Let's see some of its usag
4 min read
Retrieving And Updating Data Contained in Shelve in Python
In Python shelve you access the keys randomly. In order to access the keys randomly in python shelve we use open() function. This function works a lot like the file open() function in File handling. Syntax for open the file using Python shelve shelve.open(filename, flag='c' , writeback=True) In Order to access the keys randomly in shelve in Python,
3 min read
Updating dependencies in Python Poetry
Updating dependencies in Python Poetry is straightforward and efficient, thanks to its comprehensive command-line interface and powerful dependency resolution capabilities. Regularly updating your dependencies ensures that your project benefits from the latest features, bug fixes, and security improvements. Why Update Dependencies in Python Poetry?
3 min read
PyQt5 QCalendarWidget - Updating it
In this article we will see how we can update the QCalendarWidget. Updating means to reflect any changes that has occur at the back-end to the screen. Updating has no effect if it is disabled. In order to do this we will use update method with the QCalendarWidget object. Syntax : calendar.update() Argument : It takes no argument Return : It returns
1 min read
PyQt5 QCalendarWidget - Updating Micro focus
In this article we will see how we can update the micro focus QCalendarWidget. There are many focus i.e focus reason available like backtab, popup reason etc. Setting focus is the overloaded function, and we can reflect the change in micro focus by updating it. In order to do this we will use updateMicroFocus method with the QCalendarWidget object.
2 min read
PYGLET – Updating Sprite
In this article we will see how we can update the sprite in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire scre
3 min read
PYGLET – Manually Updating Texture of Media in Player
In this article we will see how we can update texture of media manually in player of PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be s
3 min read
Django ORM - Inserting, Updating & Deleting Data
Django lets us interact with its database models, i.e. add, delete, modify, and query objects, using a database-abstraction API called ORM(Object Relational Mapper). This article discusses all the functional operations we can perform using Django ORM. Prerequisite: Django models Django ORM Django's Object-Relational Mapping (ORM) system, a fundamen
3 min read
Dynamically Updating Plot In Matplotlib
This article shows how to dynamically update a Matplotlib (a data visualization library for Python programming language) plot as the data changes. It provides two methods to plot - first the API (useful for large programs and/or ones requiring deep control) and second the Pyplot interface (inspired by Matlab). In this article, we will show how to u
5 min read
Article Tags :
Practice Tags :