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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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}
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.