BongoBinary

How to merge dictionary together in python version 3.5 and later

By Aloyce Bahati1 year ago4 minutes read

In this post we are going to see how to merge dictionary in python, specifically in python version 3.5 and later

so the technique for python 3.5 and later is very simple. You have to create new dictionaries and include the keys and values for each dictionay. 

let say we have two dictionaries: 

dictA = {"name":"Sophia","age":12"}

dictB  ={"location":"Unknown","school":"Boston"}

and dictionary c as dictC which which take keys and values from both dictionary dictA and dictB.

to take keys and values from dictionary dictA all we have to type is **dictA and to take keys and values from dictB all we have to type is **dictB

so dictionary dictC should be written as 

dictC = {**dictA, **dictB}

if we print dictC we will get results of combined dictionary.

All in one place.

#Merging dictionary in python 3.5 and later
dictA = {"name":"Sophia","age":12}
dictB = {"school":"Boston College","location":"USA"}

#python 3.5 and later
dictC = {**dictA,**dictB}
print(dictC)

 

If you would like to know how to merge dictionaries together in lower version of python you may consider to check this tutorial Merge dictionaries in lower version of python