cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Machine Learning
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to merge two dict in python?

Kaniz
Community Manager
Community Manager
 
1 ACCEPTED SOLUTION

Accepted Solutions

Deepak_Bhutada
Contributor III

Hi @Kaniz Fatmaโ€‹ 

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 discuss a few ways of merging dictionaries. 

Using the method update() 

By using the method update() in Python, one list can be merged into another. But in this, the second list is merged into the first list and no new list is created. It returns None

# Python code to merge dict using update() method
def Merge(dict1, dict2):
    return(dict2.update(dict1))
     
# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}
 
# This return None
print(Merge(dict1, dict2))
 
# changes made in dict2
print(dict2)

Output:

None

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

Using ** in Python 

This is generally considered a trick in Python where a single expression is used to merge two dictionaries and stored in a third dictionary. The single expression is **. This does not affect the other two dictionaries. ** implies that an argument is a dictionary. Using ** [double star] is a shortcut that allows you to pass multiple arguments to a function directly using a dictionary. For more information refer **kwargs in Python. Using this we first pass all the elements of the first dictionary into the third one and then pass the second dictionary into the third. This will replace the duplicate keys of the first dictionary. 

# Python code to merge dict using a single
# expression
def Merge(dict1, dict2):
	res = {**dict1, **dict2}
	return res
	
# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}
dict3 = Merge(dict1, dict2)
print(dict3)

Output:

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

Using | in Python 3.9

In the latest update of python now we can use โ€œ|โ€ operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

# code
# Python code to merge dict using a single
# expression
def Merge(dict1, dict2):
	res = dict1 | dict2
	return res
	
# Driver code
dict1 = {'x': 10, 'y': 8}
dict2 = {'a': 6, 'b': 4}
dict3 = Merge(dict1, dict2)
print(dict3)
 
# This code is contributed by virentanti16

Output:

{'x': 10, 'a': 6, 'b': 4, 'y': 8}

View solution in original post

3 REPLIES 3

Anonymous
Not applicable

You could use "update' to do this. There may be other ways but I know 'update' works.

def test(dict1, dict2):

return (dict2.update(dict1))

dict1 = {'x': 5, 'z': 2}

dict2 = {'a':3, 'b':4}

print (test(dict1, dict2))

print (dict2)

Deepak_Bhutada
Contributor III

Hi @Kaniz Fatmaโ€‹ 

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 discuss a few ways of merging dictionaries. 

Using the method update() 

By using the method update() in Python, one list can be merged into another. But in this, the second list is merged into the first list and no new list is created. It returns None

# Python code to merge dict using update() method
def Merge(dict1, dict2):
    return(dict2.update(dict1))
     
# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}
 
# This return None
print(Merge(dict1, dict2))
 
# changes made in dict2
print(dict2)

Output:

None

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

Using ** in Python 

This is generally considered a trick in Python where a single expression is used to merge two dictionaries and stored in a third dictionary. The single expression is **. This does not affect the other two dictionaries. ** implies that an argument is a dictionary. Using ** [double star] is a shortcut that allows you to pass multiple arguments to a function directly using a dictionary. For more information refer **kwargs in Python. Using this we first pass all the elements of the first dictionary into the third one and then pass the second dictionary into the third. This will replace the duplicate keys of the first dictionary. 

# Python code to merge dict using a single
# expression
def Merge(dict1, dict2):
	res = {**dict1, **dict2}
	return res
	
# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}
dict3 = Merge(dict1, dict2)
print(dict3)

Output:

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

Using | in Python 3.9

In the latest update of python now we can use โ€œ|โ€ operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

# code
# Python code to merge dict using a single
# expression
def Merge(dict1, dict2):
	res = dict1 | dict2
	return res
	
# Driver code
dict1 = {'x': 10, 'y': 8}
dict2 = {'a': 6, 'b': 4}
dict3 = Merge(dict1, dict2)
print(dict3)
 
# This code is contributed by virentanti16

Output:

{'x': 10, 'a': 6, 'b': 4, 'y': 8}

armen23
New Contributor II

Code To Merge 2 dict in Python is:

def Merge(dict1, dict2):

    return(dict2.update(dict1))

     

# Driver code

dict1 = {'a': 10, 'b': 8}

dict2 = {'d': 6, 'c': 4}

# This return None

print(Merge(dict1, dict2))

# changes made in dict2

print(dict2)

For More Visit: Python classes in Pune

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.