BongoBinary

How to merge dictionaries together in lower version of python

By Aloyce Bahati1 year ago5 minutes read

In this post we are going to see how to merge dictionary in python, specifically in lower version of python

the technique for merging dictionaries in lower version of python is to copy one of the dictionary into a new dictionary then update the new dictionary using the remaining dictionaries.

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.

#copy dictionary dictA into dictC

dictC = dictA.copy()

# include dictionary dictB in dictionary B

dictC.update(dictB)

print(dictC)

 

All in one place.

#Merging dictionary in python 3.4 and lower
a = {"name":"Sophia","age":12}
b = {"school":"Boston College","location":"USA"}


#python 3.4 and lower
c = a.copy()
c.update(b)

print(c)

To Learn how to merge dictionaries only on python version 3.5 and later check this link MERGE DICTIONARIES ON LATER VERSION OF PYTHON