Easy Tutorial
❮ Ref Set Isdisjoint Ref Set Union ❯

Python Merging Dictionaries

Python3 Examples

Given two dictionaries, merge them into one dictionary.

Example 1: Using the update() method, the second parameter merges into the first parameter

def Merge(dict1, dict2): 
    return(dict2.update(dict1)) 

# Two dictionaries
dict1 = {'a': 10, 'b': 8} 
dict2 = {'d': 6, 'c': 4} 

# Returns None
print(Merge(dict1, dict2)) 

# dict2 is merged with dict1
print(dict2)

Executing the above code outputs:

None
{'d': 6, 'c': 4, 'a': 10, 'b': 8}

Example 2: Using **, the function imports the parameters in dictionary form

def Merge(dict1, dict2): 
    res = {**dict1, **dict2} 
    return res 

# Two dictionaries
dict1 = {'a': 10, 'b': 8} 
dict2 = {'d': 6, 'c': 4} 
dict3 = Merge(dict1, dict2) 
print(dict3)

Executing the above code outputs:

{'a': 10, 'b': 8, 'd': 6, 'c': 4}

Python3 Examples

❮ Ref Set Isdisjoint Ref Set Union ❯