Exercise 101

We consider the three Pythons dictionaries which includes all the computer hardwares:

dicPC = {"HP": 11, "Acer": 7, "Lenovo": 17, "Del": 23}
dicPhone = {"Sumsung": 22, "Iphone": 9, "Other": 13}
dicTablet = {"Sumsung": 15, "Other": 13}

Write a Python program that combines by concatenating these three dictionaries into one.

Solution

dicPC = {"HP": 11, "Acer": 7, "Lenovo": 17, "Del": 23}
dicPhone = {"Sumsung": 22, "Iphone": 9, "Other": 13}
dicTablet = {"Sumsung": 15, "Other": 13}
# we create a dictionary which will contain the others dictionary
dicTotal = {}
# we add the dictionaries via the for loop
for d in [dicPC, dicTablet, dicPhone]:
dicTotal.update (d)

print (dicTotal)
# The output is : {'HP': 11, 'Acer': 7, 'Lenovo': 17, 'Del': 23, 'Sumsung': 22, 'Other': 13, 'Iphone': 9}

Younes Derfoufi
my-courses.net
One thought on “Solution Exercise 101: python code to concatenate dictionaries”

Leave a Reply